wpf展开树节点_【转】WPF TreeView如何展开到某个节点

本文介绍了在WPF中如何展开TreeView到特定节点。关键在于获取从根节点到目标节点的路径,并逐级展开父节点。通过ItemsControl.ItemContainerGenerator.ContainerFromItem(item)方法,可以获取数据源T对应的TreeViewItem。如果ItemsControl.ItemContainerGenerator.Status为ContainersGenerated,直接设置IsExpanded为true,否则需调用UpdateLayout()。文中提供了一个示例,展示了如何实现这一功能。
摘要由CSDN通过智能技术生成

初用WPF的TreeView控件,需要将树展开到某个特定的TreeViewItem,各种方法都尝试过,却发现代码总在某些情况下出错,然后仔细研究,才发现其中的曲折。

解决问题的思路是,得到从树的根节点到特定节点的路线,并打开所有父节点。但是曲折的地方就是如何得到下一级的子节点,也就是如何从Items集合中取得对应的TreeViewItem并set IsExpanded = true。

TreeView的Items集合和TreeViewItem的Items集合都是从ItemsControl父类继承过来的,在这个集合中,其实可能存放两种对象。其一是TreeViewItem,或者就是绑定数据的数据源。也就是根据XAML的不同定义会存储不同的数据,所以使用起来要特别小心。如果是使用 tag或者new TreeViewItem() 方式添加节点,那么Items集合中当然是TreeViewItem。如果是使用ItemsSource绑定ObservableCollection的话,那么你拿到的将会是数据源T。

那么我们着重讨论第二种情况,也是最常用的情况。当你拿到一个T对象以后,如何得到所对应的TreeViewItem。这就是需要调用ItemsControl.ItemContainerGenerator.ContainerFromItem(itemT) as TreeViewItem这种方式,而且这个方法还有一点好处,就是如果你传入的不是itemT,而是TreeViewItem,它也会返回其本身,所以就不需要顾虑上面所讲的两种在Items集合中不同的对象。但是这个方法并不是随时都可以调用的。WPF为了考虑性能,所以如果使用绑定方式的TreeViewItem,只要在该节点需要被展现时才会被产生,所以在调用ContainerFromItem之前,需要检查ItemsControl.ItemContainerGenerator.Status,如果等于ContainersGenerated,那么说明子节点已经被产生了,否则需要调用ItemsControl.UpdateLayout()方法,让其产生子节点。

现在所有的问题已经解决了,我给一段小例子吧。

首先定义绑定数据类型。

1 public classHierarchyItem : INotifyPropertyChanged2 {3 public HierarchyCollection Children { get; set; }4

5 public HierarchyItem Parent { get; set; }6

7 public string Name { get; set; }8

9 publicHierarchyItem()10 {11 Children = newHierarchyCollection();12 }13

14 public eventPropertyChangedEventHandler PropertyChanged;15 }16

17 public class HierarchyCollection : ObservableCollection

18 {19

20 }

然后准备数据源,并绑定到你的TreeView控件上去。

1 privateHierarchyCollection PrepareCollection()2 {3   HierarchyCollection collection = newHierarchyCollection();4   HierarchyItem a1 = new HierarchyItem() { Name = "A1"};5   collection.Add(a1);6   HierarchyItem b1 = new HierarchyItem() { Name = "B1", Parent =a1 };7 a1.Children.Add(b1);8 HierarchyItem b2 = new HierarchyItem() { Name = "B2", Parent =a1 };9 a1.Children.Add(b2);10 HierarchyItem b3 = new HierarchyItem() { Name = "B3", Parent =a1 };11 a1.Children.Add(b3);12 HierarchyItem c1 = new HierarchyItem() { Name = "C1", Parent =b1 };13 b1.Children.Add(c1);14 HierarchyItem c2 = new HierarchyItem() { Name = "C2", Parent =b1 };15 b1.Children.Add(c2);16 HierarchyItem c3 = new HierarchyItem() { Name = "C3", Parent =b1 };18 b1.Children.Add(c3);

20 HierarchyItem c4 = new HierarchyItem() { Name = "C4", Parent =b2 };21 b2.Children.Add(c4);22 HierarchyItem c5 = new HierarchyItem() { Name = "C5", Parent =b2 };24 b2.Children.Add(c5);25 returncollection;26 }

最后就是展开的代码了,在某个button的click处理函数中。

1 private void Button_Click(objectsender, RoutedEventArgs e)2 {3   //A specific node

4 HierarchyItem item = collection[0].Children[1].Children[1];5 List pendingSelectionPath = new List();6 while (item.Parent != null)7 {8 pendingSelectionPath.Insert(0, item.Parent);9 item =item.Parent;10 }11

12 private void ExpandToPendingSelection(ListpendingSelectionPath)13 {14   //Grabs the treeview control.

15   var itemsControl = testTreeView asItemsControl;16   foreach (HierarchyItem item inpendingSelectionPath)17   {18     TreeViewItem container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) asTreeViewItem;19     if (container != null)20     {21       container.IsExpanded = true;22       if (container.ItemContainerGenerator.Status !=System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)23       {24         container.UpdateLayout();25       }26       itemsControl =container;27     }28}29 }

这样就可以了,其实我不喜欢在blog里给出大量的代码,原因我也说过。这次也是尝试一下,如果有人喜欢,那么以后的技术类文章我也尽量给出一些代码。其实这个问题还有一种更简单的实现方式,提示一下,用IsExpanded这个DP来一个TwoWay的Binding。大家可以去试试看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值