先看效果

TreeView中是自带有滚动条的,左右滚动和上下滚动,TreeView的内部是很复杂的。
使用snoop软件可以看到,有一个上下滚动,和一个左右滚动
上下滚动

左右滚动

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

2.xaml文件代码
"TreeView.MainWindow" - 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:TreeView"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
-
-
-
-
-
-
"Red" Width="200" Height="200" HorizontalAlignment="Left"> -
"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" > -
-
"Hidden" /> -
-
-
-
-
-
3.后台cs文件代码
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace TreeView
- {
- ///
- /// MainWindow.xaml 的交互逻辑
- ///
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- List
persons = new List(); - Person person1 = new Person() { Name = "John DoeJohn DoeJohn Doe", Age = 42 };
- Person person2 = new Person() { Name = "Jane DoeJohn DoeJohn Doe", Age = 39 };
- Person child1 = new Person() { Name = "Sammy DoeJohn DoeJohn Doe", Age = 13 };
- person1.Children.Add(child1);
- person2.Children.Add(child1);
- person2.Children.Add(new Person() { Name = "Jenny MoeJohn Doe", Age = 17 });
- Person person3 = new Person() { Name = "Becky ToJohn DoeJohn Doe", Age = 25 };
- persons.Add(person1);
- persons.Add(person2);
- persons.Add(person3);
- persons.Add(person1);
- persons.Add(person2);
- persons.Add(person3);
- persons.Add(person1);
- persons.Add(person2);
- persons.Add(person3);
- persons.Add(person1);
- persons.Add(person2);
- persons.Add(person3);
- persons.Add(person1);
- persons.Add(person2);
- persons.Add(person3);
- person2.IsExpanded = true;
- person2.IsSelected = true;
- trvPersons.ItemsSource = persons;
- }
- }
-
- public class Person : TreeViewItemBase
- {
- public Person()
- {
- this.Children = new ObservableCollection
(); - }
- public string Name { get; set; }
- public int Age { get; set; }
- public ObservableCollection
Children { get; set; } - }
-
-
- public class TreeViewItemBase : INotifyPropertyChanged
- {
- private bool isSelected;
- public bool IsSelected
- {
- get { return this.isSelected; }
- set
- {
- if (value != this.isSelected)
- {
- this.isSelected = value;
- NotifyPropertyChanged("IsSelected");
- }
- }
- }
- private bool isExpanded;
- public bool IsExpanded
- {
- get { return this.isExpanded; }
- set
- {
- if (value != this.isExpanded)
- {
- this.isExpanded = value;
- NotifyPropertyChanged("IsExpanded");
- }
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void NotifyPropertyChanged(string propName)
- {
- if (this.PropertyChanged != null)
- this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
- }
- }
- }
4.ScrollViewer.xaml样式文件
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" - xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
5.配置App.xaml
"TreeView.App" - xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:TreeView"
- StartupUri="MainWindow.xaml">
-
-
-
-
"pack://application:,,,/TreeView;component/ScrollViewer.xaml"/> -
-
-
6.效果


拓展
展开和收缩功能,直接传递TreeView控件和true或者false
- private void SetNodeExpandedState(ItemsControl control, bool expandNode)
- {
- try
- {
- if (control != null)
- {
- foreach (object item in control.Items)
- {
- TreeViewItem treeItem = control.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
-
- if (treeItem != null && treeItem.HasItems)
- {
- treeItem.IsExpanded = expandNode;
-
- if (treeItem.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
- {
- treeItem.UpdateLayout();
- }
-
- SetNodeExpandedState(treeItem as ItemsControl, expandNode);
- }
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- }
上面操作是某一个界面使用的
如果需要全局引用,去掉界面上面的代码
打开ScrollViewer.xaml的代码即可
-
-