翼度科技»论坛 编程开发 .net 查看内容

WPF实现树形表格控件(TreeListView)

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
前言
  本文将探讨如何利用WPF框架实现树形表格控件,该控件不仅能够有效地展示复杂的层级数据,还能够提供丰富的个性化定制选项。我们将介绍如何使用WPF提供的控件、模板、布局、数据绑定等技术来构建这样一个树形表格。
一、运行效果
1.1默认样式

1.2 自定义样式

二、代码实现
2.1 创建自定义控件(TreeListView)
      新建一个继承自TreeView的控件,并定义一个类型为ViewBase的View依赖属性,用于在代码中指定列。
  1. public class TreeListView : TreeView
  2. {
  3. <Trigger Property="IsExpanded" Value="false">
  4.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  5. </Trigger><Trigger Property="IsExpanded" Value="false">
  6.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  7. </Trigger>public ViewBase View
  8. <Trigger Property="IsExpanded" Value="false">
  9.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  10. </Trigger><Trigger Property="IsExpanded" Value="false">
  11.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  12. </Trigger>{
  13. <Trigger Property="IsExpanded" Value="false">
  14.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  15. </Trigger><Trigger Property="IsExpanded" Value="false">
  16.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  17. </Trigger><Trigger Property="IsExpanded" Value="false">
  18.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  19. </Trigger>get { return (ViewBase)GetValue(ViewProperty); }
  20. <Trigger Property="IsExpanded" Value="false">
  21.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  22. </Trigger><Trigger Property="IsExpanded" Value="false">
  23.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  24. </Trigger><Trigger Property="IsExpanded" Value="false">
  25.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  26. </Trigger>set { SetValue(ViewProperty, value); }
  27. <Trigger Property="IsExpanded" Value="false">
  28.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  29. </Trigger><Trigger Property="IsExpanded" Value="false">
  30.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  31. </Trigger>}
  32. <Trigger Property="IsExpanded" Value="false">
  33.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  34. </Trigger><Trigger Property="IsExpanded" Value="false">
  35.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  36. </Trigger>public static readonly DependencyProperty ViewProperty =
  37. <Trigger Property="IsExpanded" Value="false">
  38.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  39. </Trigger><Trigger Property="IsExpanded" Value="false">
  40.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  41. </Trigger><Trigger Property="IsExpanded" Value="false">
  42.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  43. </Trigger>DependencyProperty.Register("View", typeof(ViewBase), typeof(TreeListView));
  44. }
复制代码
2.2 在TreeListView控件模板中处理列头
   为了在TreeListView中显示列头,需要在合适的位置添加GridViewHeaderRowPresenter控件,并在Columns属性上绑定我们之前定义的View.Columns属性。下面我们首先来分析TreeView控件模板的代码。
  1. <ControlTemplate TargetType="{x:Type TreeView}">
  2. <Trigger Property="IsExpanded" Value="false">
  3.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  4. </Trigger><Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
  5. <Trigger Property="IsExpanded" Value="false">
  6.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  7. </Trigger><Trigger Property="IsExpanded" Value="false">
  8.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  9. </Trigger><ScrollViewer x:Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" CanContentScroll="false" Focusable="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
  10. <Trigger Property="IsExpanded" Value="false">
  11.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  12. </Trigger><Trigger Property="IsExpanded" Value="false">
  13.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  14. </Trigger><Trigger Property="IsExpanded" Value="false">
  15.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  16. </Trigger><ItemsPresenter/>
  17. <Trigger Property="IsExpanded" Value="false">
  18.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  19. </Trigger><Trigger Property="IsExpanded" Value="false">
  20.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  21. </Trigger></ScrollViewer>
  22. <Trigger Property="IsExpanded" Value="false">
  23.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  24. </Trigger></Border>
  25. <Trigger Property="IsExpanded" Value="false">
  26.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  27. </Trigger><ControlTemplate.Triggers>
  28. <Trigger Property="IsExpanded" Value="false">
  29.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  30. </Trigger><Trigger Property="IsExpanded" Value="false">
  31.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  32. </Trigger><Trigger Property="IsEnabled" Value="false">
  33. <Trigger Property="IsExpanded" Value="false">
  34.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  35. </Trigger><Trigger Property="IsExpanded" Value="false">
  36.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  37. </Trigger><Trigger Property="IsExpanded" Value="false">
  38.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  39. </Trigger><Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  40. <Trigger Property="IsExpanded" Value="false">
  41.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  42. </Trigger><Trigger Property="IsExpanded" Value="false">
  43.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  44. </Trigger></Trigger>
  45. <Trigger Property="IsExpanded" Value="false">
  46.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  47. </Trigger><Trigger Property="IsExpanded" Value="false">
  48.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  49. </Trigger><Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
  50. <Trigger Property="IsExpanded" Value="false">
  51.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  52. </Trigger><Trigger Property="IsExpanded" Value="false">
  53.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  54. </Trigger><Trigger Property="IsExpanded" Value="false">
  55.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  56. </Trigger><Setter Property="CanContentScroll" TargetName="_tv_scrollviewer_" Value="true"/>
  57. <Trigger Property="IsExpanded" Value="false">
  58.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  59. </Trigger><Trigger Property="IsExpanded" Value="false">
  60.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  61. </Trigger></Trigger>
  62. <Trigger Property="IsExpanded" Value="false">
  63.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  64. </Trigger></ControlTemplate.Triggers>
  65. </ControlTemplate>
复制代码
      通过以上代码我们可以看出,只要将GridViewHeaderRowPresenter控件添加到ScrollViewer控件上面即可实现列头功能,但这样会有一个问题,那就是内容宽度超出控件宽度后,鼠标拖动横向滚动条时列头不会跟随下方的数据列表一起滚动。为解决这个问题我们需要将GridViewHeaderRowPresenter放置到ScrollViewer控件模板中,以下为完整代码。
  1. [/code][b]2.3 在TreeListViewItem模板中处理子项的展开和收缩[/b]
  2.       新建一个继承自TreeViewItem的类,命名为TreeListViewItem(如有个性化需求,可以在该类中处理),编辑控件模板,在模板中添加以下代码。
  3. [code]
复制代码
    此处的核心在于模板中添加了GridViewRowPresenter控件,并在Columns属性上绑定了我们之前定义的View.Columns属性,这样就可以在每一行上面显示列数据。还有一个关键点是ItemsPresenter,它用于显示子项数据,此处命名为ItemsHost,它由属性触发器中的代码来控件展开和收起。以下是属性触发器代码。
  1. <Trigger Property="IsExpanded" Value="false">
  2.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  3. </Trigger>
复制代码
2.4 在单元格模板中控件子项的展开与收起
   为了达到展开和收起的效果,需要在首列的单元格中控制TreeListViewItem的IsExpanded属性。以下为完整代码。
  1. <Trigger Property="IsExpanded" Value="false">
  2.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  3. </Trigger><Trigger Property="IsExpanded" Value="false">
  4.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  5. </Trigger><Trigger Property="IsExpanded" Value="false">
  6.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  7. </Trigger><Trigger Property="IsExpanded" Value="false">
  8.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  9. </Trigger><Trigger Property="IsExpanded" Value="false">
  10.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  11. </Trigger><Trigger Property="IsExpanded" Value="false">
  12.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  13. </Trigger><Trigger Property="IsExpanded" Value="false">
  14.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  15. </Trigger><Trigger Property="IsExpanded" Value="false">
  16.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  17. </Trigger><Trigger Property="IsExpanded" Value="false">
  18.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  19. </Trigger><Trigger Property="IsExpanded" Value="false">
  20.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  21. </Trigger><Trigger Property="IsExpanded" Value="false">
  22.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  23. </Trigger><Trigger Property="IsExpanded" Value="false">
  24.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  25. </Trigger><Trigger Property="IsExpanded" Value="false">
  26.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  27. </Trigger><Trigger Property="IsExpanded" Value="false">
  28.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  29. </Trigger><Trigger Property="IsExpanded" Value="false">
  30.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  31. </Trigger>
复制代码
其关键代码为
  1. IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeListViewItem}}}"
复制代码
2.5 控件使用
  1. <ControlTemplate TargetType="{x:Type TreeView}">
  2. <Trigger Property="IsExpanded" Value="false">
  3.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  4. </Trigger><Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
  5. <Trigger Property="IsExpanded" Value="false">
  6.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  7. </Trigger><Trigger Property="IsExpanded" Value="false">
  8.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  9. </Trigger><ScrollViewer x:Name="_tv_scrollviewer_" Background="{TemplateBinding Background}" CanContentScroll="false" Focusable="false" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
  10. <Trigger Property="IsExpanded" Value="false">
  11.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  12. </Trigger><Trigger Property="IsExpanded" Value="false">
  13.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  14. </Trigger><Trigger Property="IsExpanded" Value="false">
  15.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  16. </Trigger><ItemsPresenter/>
  17. <Trigger Property="IsExpanded" Value="false">
  18.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  19. </Trigger><Trigger Property="IsExpanded" Value="false">
  20.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  21. </Trigger></ScrollViewer>
  22. <Trigger Property="IsExpanded" Value="false">
  23.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  24. </Trigger></Border>
  25. <Trigger Property="IsExpanded" Value="false">
  26.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  27. </Trigger><ControlTemplate.Triggers>
  28. <Trigger Property="IsExpanded" Value="false">
  29.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  30. </Trigger><Trigger Property="IsExpanded" Value="false">
  31.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  32. </Trigger><Trigger Property="IsEnabled" Value="false">
  33. <Trigger Property="IsExpanded" Value="false">
  34.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  35. </Trigger><Trigger Property="IsExpanded" Value="false">
  36.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  37. </Trigger><Trigger Property="IsExpanded" Value="false">
  38.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  39. </Trigger><Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  40. <Trigger Property="IsExpanded" Value="false">
  41.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  42. </Trigger><Trigger Property="IsExpanded" Value="false">
  43.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  44. </Trigger></Trigger>
  45. <Trigger Property="IsExpanded" Value="false">
  46.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  47. </Trigger><Trigger Property="IsExpanded" Value="false">
  48.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  49. </Trigger><Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
  50. <Trigger Property="IsExpanded" Value="false">
  51.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  52. </Trigger><Trigger Property="IsExpanded" Value="false">
  53.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  54. </Trigger><Trigger Property="IsExpanded" Value="false">
  55.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  56. </Trigger><Setter Property="CanContentScroll" TargetName="_tv_scrollviewer_" Value="true"/>
  57. <Trigger Property="IsExpanded" Value="false">
  58.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  59. </Trigger><Trigger Property="IsExpanded" Value="false">
  60.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  61. </Trigger></Trigger>
  62. <Trigger Property="IsExpanded" Value="false">
  63.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  64. </Trigger></ControlTemplate.Triggers>
  65. </ControlTemplate><Trigger Property="IsExpanded" Value="false">
  66.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  67. </Trigger><Trigger Property="IsExpanded" Value="false">
  68.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  69. </Trigger><Trigger Property="IsExpanded" Value="false">
  70.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  71. </Trigger><Trigger Property="IsExpanded" Value="false">
  72.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  73. </Trigger><Trigger Property="IsExpanded" Value="false">
  74.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  75. </Trigger><Trigger Property="IsExpanded" Value="false">
  76.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  77. </Trigger><Trigger Property="IsExpanded" Value="false">
  78.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  79. </Trigger><Trigger Property="IsExpanded" Value="false">
  80.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  81. </Trigger><Trigger Property="IsExpanded" Value="false">
  82.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  83. </Trigger><Trigger Property="IsExpanded" Value="false">
  84.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  85. </Trigger><Trigger Property="IsExpanded" Value="false">
  86.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  87. </Trigger><Trigger Property="IsExpanded" Value="false">
  88.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  89. </Trigger><Trigger Property="IsExpanded" Value="false">
  90.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  91. </Trigger><Trigger Property="IsExpanded" Value="false">
  92.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  93. </Trigger><Trigger Property="IsExpanded" Value="false">
  94.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  95. </Trigger><Trigger Property="IsExpanded" Value="false">
  96.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  97. </Trigger><Trigger Property="IsExpanded" Value="false">
  98.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  99. </Trigger><Trigger Property="IsExpanded" Value="false">
  100.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  101. </Trigger><Trigger Property="IsExpanded" Value="false">
  102.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  103. </Trigger><Trigger Property="IsExpanded" Value="false">
  104.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  105. </Trigger><Trigger Property="IsExpanded" Value="false">
  106.     <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
  107. </Trigger>
复制代码
 
 技术交流群
  联系方式

来源:https://www.cnblogs.com/qushi2020/p/18109923
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具