【转】WPF TreeView如何展开到某个节点
初用WPF的TreeView控件,需要将树展开到某个特定的TreeViewItem,各种方法都尝试过,却发现代码总在某些情况下出错,然后仔细研究,才发现其中的曲折。
解决问题的思路是,得到从树的根节点到特定节点的路线,并打开所有父节点。但是曲折的地方就是如何得到下一级的子节点,也就是如何从Items集合中取得对应的TreeViewItem并set IsExpanded = true。
TreeView的Items集合和TreeViewItem的Items集合都是从ItemsControl父类继承过来的,在这个集合中,其实可能存放两种对象。其一是TreeViewItem,或者就是绑定数据的数据源。也就是根据XAML的不同定义会存储不同的数据,所以使用起来要特别小心。如果是使用<TreeViewItem /> tag或者new TreeViewItem() 方式添加节点,那么Items集合中当然是TreeViewItem。如果是使用ItemsSource绑定ObservableCollection<T>的话,那么你拿到的将会是数据源T。
那么我们着重讨论第二种情况,也是最常用的情况。当你拿到一个T对象以后,如何得到所对应的TreeViewItem。这就是需要调用ItemsControl.ItemContainerGenerator.ContainerFromItem(itemT) as TreeViewItem这种方式,而且这个方法还有一点好处,就是如果你传入的不是itemT,而是TreeViewItem,它也会返回其本身,所以就不需要顾虑上面所讲的两种在Items集合中不同的对象。但是这个方法并不是随时都可以调用的。WPF为了考虑性能,所以如果使用绑定方式的TreeViewItem,只要在该节点需要被展现时才会被产生,所以在调用ContainerFromItem之前,需要检查ItemsControl.ItemContainerGenerator.Status,如果等于ContainersGenerated,那么说明子节点已经被产生了,否则需要调用ItemsControl.UpdateLayout()方法,让其产生子节点。
现在所有的问题已经解决了,我给一段小例子吧。
首先定义绑定数据类型。
1 public class HierarchyItem : INotifyPropertyChanged 2 { 3 public HierarchyCollection Children { get; set; } 4 5 public HierarchyItem Parent { get; set; } 6 7 public string Name { get; set; } 8 9 public HierarchyItem() 10 { 11 Children = new HierarchyCollection(); 12 } 13 14 public event PropertyChangedEventHandler PropertyChanged; 15 } 16 17 public class HierarchyCollection : ObservableCollection<HierarchyItem> 18 { 19 20 }
然后准备数据源,并绑定到你的TreeView控件上去。
1 private HierarchyCollection PrepareCollection() 2 { 3 HierarchyCollection collection = new HierarchyCollection(); 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 return collection; 26 }
最后就是展开的代码了,在某个button的click处理函数中。
1 private void Button_Click(object sender, RoutedEventArgs e) 2 { 3 // A specific node 4 HierarchyItem item = collection[0].Children[1].Children[1]; 5 List<HierarchyItem> pendingSelectionPath = new List<HierarchyItem>(); 6 while (item.Parent != null) 7 { 8 pendingSelectionPath.Insert(0, item.Parent); 9 item = item.Parent; 10 } 11 12 private void ExpandToPendingSelection(List<HierarchyItem> pendingSelectionPath) 13 { 14 // Grabs the treeview control. 15 var itemsControl = testTreeView as ItemsControl; 16 foreach (HierarchyItem item in pendingSelectionPath) 17 { 18 TreeViewItem container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem; 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。大家可以去试试看。
原文链接 https://blog.csdn.net/lunasea0_0/article/details/6210501