• wpf中TreeView的滚动条


    先看效果

    TreeView中是自带有滚动条的,左右滚动和上下滚动,TreeView的内部是很复杂的。

    使用snoop软件可以看到,有一个上下滚动,和一个左右滚动

    上下滚动

     左右滚动

    1.建立一个wpf项目,文件总览 

    2.xaml文件代码

    1. "TreeView.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:TreeView"
    7. mc:Ignorable="d"
    8. Title="MainWindow" Height="450" Width="800">
    9. "Red" Width="200" Height="200" HorizontalAlignment="Left">
    10. "verticalScrollBarForModuleTree" ViewportSize="1" FontSize="40" MinWidth="30" MinHeight="50" Margin="0" Background="#FF10516D" Width="20" HorizontalAlignment="Right" Height="200" Foreground="#00E81212" BorderBrush="#00E01414" OpacityMask="#FFFF1818" SmallChange="10" >
    11. "Hidden" />

    3.后台cs文件代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Collections.ObjectModel;
    4. using System.ComponentModel;
    5. using System.Linq;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. using System.Windows;
    9. using System.Windows.Controls;
    10. using System.Windows.Data;
    11. using System.Windows.Documents;
    12. using System.Windows.Input;
    13. using System.Windows.Media;
    14. using System.Windows.Media.Imaging;
    15. using System.Windows.Navigation;
    16. using System.Windows.Shapes;
    17. namespace TreeView
    18. {
    19. ///
    20. /// MainWindow.xaml 的交互逻辑
    21. ///
    22. public partial class MainWindow : Window
    23. {
    24. public MainWindow()
    25. {
    26. InitializeComponent();
    27. List persons = new List();
    28. Person person1 = new Person() { Name = "John DoeJohn DoeJohn Doe", Age = 42 };
    29. Person person2 = new Person() { Name = "Jane DoeJohn DoeJohn Doe", Age = 39 };
    30. Person child1 = new Person() { Name = "Sammy DoeJohn DoeJohn Doe", Age = 13 };
    31. person1.Children.Add(child1);
    32. person2.Children.Add(child1);
    33. person2.Children.Add(new Person() { Name = "Jenny MoeJohn Doe", Age = 17 });
    34. Person person3 = new Person() { Name = "Becky ToJohn DoeJohn Doe", Age = 25 };
    35. persons.Add(person1);
    36. persons.Add(person2);
    37. persons.Add(person3);
    38. persons.Add(person1);
    39. persons.Add(person2);
    40. persons.Add(person3);
    41. persons.Add(person1);
    42. persons.Add(person2);
    43. persons.Add(person3);
    44. persons.Add(person1);
    45. persons.Add(person2);
    46. persons.Add(person3);
    47. persons.Add(person1);
    48. persons.Add(person2);
    49. persons.Add(person3);
    50. person2.IsExpanded = true;
    51. person2.IsSelected = true;
    52. trvPersons.ItemsSource = persons;
    53. }
    54. }
    55. public class Person : TreeViewItemBase
    56. {
    57. public Person()
    58. {
    59. this.Children = new ObservableCollection();
    60. }
    61. public string Name { get; set; }
    62. public int Age { get; set; }
    63. public ObservableCollection Children { get; set; }
    64. }
    65. public class TreeViewItemBase : INotifyPropertyChanged
    66. {
    67. private bool isSelected;
    68. public bool IsSelected
    69. {
    70. get { return this.isSelected; }
    71. set
    72. {
    73. if (value != this.isSelected)
    74. {
    75. this.isSelected = value;
    76. NotifyPropertyChanged("IsSelected");
    77. }
    78. }
    79. }
    80. private bool isExpanded;
    81. public bool IsExpanded
    82. {
    83. get { return this.isExpanded; }
    84. set
    85. {
    86. if (value != this.isExpanded)
    87. {
    88. this.isExpanded = value;
    89. NotifyPropertyChanged("IsExpanded");
    90. }
    91. }
    92. }
    93. public event PropertyChangedEventHandler PropertyChanged;
    94. public void NotifyPropertyChanged(string propName)
    95. {
    96. if (this.PropertyChanged != null)
    97. this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    98. }
    99. }
    100. }

    4.ScrollViewer.xaml样式文件

    1. "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    5.配置App.xaml

    1. "TreeView.App"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:local="clr-namespace:TreeView"
    5. StartupUri="MainWindow.xaml">
    6. "pack://application:,,,/TreeView;component/ScrollViewer.xaml"/>

     6.效果

    拓展

    展开和收缩功能,直接传递TreeView控件和true或者false

    1. private void SetNodeExpandedState(ItemsControl control, bool expandNode)
    2. {
    3. try
    4. {
    5. if (control != null)
    6. {
    7. foreach (object item in control.Items)
    8. {
    9. TreeViewItem treeItem = control.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
    10. if (treeItem != null && treeItem.HasItems)
    11. {
    12. treeItem.IsExpanded = expandNode;
    13. if (treeItem.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    14. {
    15. treeItem.UpdateLayout();
    16. }
    17. SetNodeExpandedState(treeItem as ItemsControl, expandNode);
    18. }
    19. }
    20. }
    21. }
    22. catch (Exception ex)
    23. {
    24. }
    25. }

    上面操作是某一个界面使用的

    如果需要全局引用,去掉界面上面的代码

    打开ScrollViewer.xaml的代码即可