WPF自定义模板--TreeView 实现菜单连接线

有些小伙伴说,在TreeView中,怎么每一个都加上连接线,进行显示连接。

代码和效果如下:

其实就是在原来的模板中增加一列显示线条,然后绘制即可

<Window x:Class="XH.TemplateLesson.TreeViewWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XH.TemplateLesson"
        mc:Ignorable="d"
        Title="TreeViewWindow" Height="450" Width="800">
<Window.Resources>
  <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="Background" Value="Transparent"/>
      <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
      <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
      <Setter Property="Padding" Value="1,0,0,0"/>
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
      <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="{x:Type TreeViewItem}">
                  <Grid>
                      <Grid.ColumnDefinitions>
                          <!--主要是放线-->
                          <ColumnDefinition Width="10"/>
                          <ColumnDefinition MinWidth="19" Width="Auto"/>
                          <ColumnDefinition Width="*"/>
                          <ColumnDefinition Width="auto"/>
                      </Grid.ColumnDefinitions>
                      <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition/>
                      </Grid.RowDefinitions>
                      <Border x:Name="Bd" 
                              Background="Transparent" 
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}" 
                              Grid.Column="1" Padding="{TemplateBinding Padding}"
                              SnapsToDevicePixels="true">
                          <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                      </Border>
                      <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="2"/>
                      <!--线区域-->
                      <!--横线-->
                      <Border BorderBrush="Green" BorderThickness="0 1 0 0" Margin="0 0 0 1.75" VerticalAlignment="Center" />
                      <!--竖线-->
                      <Border BorderBrush="Green" BorderThickness="1 0 0 0" Grid.Row="1" HorizontalAlignment="Right" Margin="0 -8 0 9.5"/>
                  </Grid>
              </ControlTemplate>
          </Setter.Value>
      </Setter>
  </Style>
</Window.Resources>
  <Grid>
    <TreeView>
        <TreeViewItem Header="AAA" IsExpanded="True">
            <TreeViewItem Header="AAAA-1"/>
            <TreeViewItem Header="AAAA-2"/>
            <TreeViewItem Header="AAAA-3"/>
            <TreeViewItem Header="AAAA-4"/>
        </TreeViewItem>
    </TreeView>
  </Grid>
</Window>

样式:

可以根据自己的需要继续调整。

相关推荐
mrchip5 小时前
Simple WPF: WPF 实现按钮的长按,短按功能
c#·wpf·.net core
mrchip16 小时前
Simple WPF: WPF 自定义按钮外形
c#·wpf
martian6651 天前
学懂C#编程:WPF应用开发系列——WPF之ComboBox控件的详细用法
开发语言·c#·wpf·控件·combobox
K哥爬虫1 天前
【0基础学爬虫】爬虫框架之 feapder 的使用
爬虫·wpf
云草桑2 天前
WPF UI 界面布局 魔术棒 文字笔记识别 技能提升 布局功能扩展与自定义 继承Panel的对象,测量与排列 系列七
ui·wpf·设计·布局·版式设计
罗迪尼亚的熔岩2 天前
使用附加属性 实现wpf中的passwordBox 的明文/密文密码切换
wpf·状态模式
小海聊工控上位机2 天前
WPF资源的使用
c#·wpf
无熵~2 天前
C#/WPF 自制白板工具
开发语言·c#·wpf
mingupup2 天前
WPF在.NET9中的重大更新:Windows 11 主题
windows·.net·wpf