轉帖|其它|編輯:郝浩|2011-03-01 13:27:10.000|閱讀 3481 次
概述:初用WPF的TreeView控件,需要將樹展開到某個特定的TreeViewItem,各種方法都嘗試過,卻發現代碼總在某些情況下出錯,然后仔細研究,才發現其中的曲折。本文主要介紹WPF TreeView如何展開到某個節點,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
初用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()方法,讓其產生子節點。
現在所有的問題已經解決了,我給一段小例子吧。
首先定義綁定數據類型。
public class HierarchyItem : INotifyPropertyChanged
{
public HierarchyCollection Children { get; set; }
public HierarchyItem Parent { get; set; }
public string Name { get; set; }
public HierarchyItem()
{
Children = new HierarchyCollection();
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class HierarchyCollection : ObservableCollection<HierarchyItem>
{
}
然后準備數據源,并綁定到你的TreeView控件上去。
private HierarchyCollection PrepareCollection()
{
HierarchyCollection collection = new HierarchyCollection();
HierarchyItem a1 = new HierarchyItem() { Name = "A1" };
collection.Add(a1);
HierarchyItem b1 = new HierarchyItem() { Name = "B1", Parent = a1 };
a1.Children.Add(b1);
HierarchyItem b2 = new HierarchyItem() { Name = "B2", Parent = a1 };
a1.Children.Add(b2);
HierarchyItem b3 = new HierarchyItem() { Name = "B3", Parent = a1 };
a1.Children.Add(b3);
HierarchyItem c1 = new HierarchyItem() { Name = "C1", Parent = b1 };
b1.Children.Add(c1);
HierarchyItem c2 = new HierarchyItem() { Name = "C2", Parent = b1 };
b1.Children.Add(c2);
HierarchyItem c3 = new HierarchyItem() { Name = "C3", Parent = b1 };
b1.Children.Add(c3);
HierarchyItem c4 = new HierarchyItem() { Name = "C4", Parent = b2 };
b2.Children.Add(c4);
HierarchyItem c5 = new HierarchyItem() { Name = "C5", Parent = b2 };
b2.Children.Add(c5);
return collection;
}
最后就是展開的代碼了,在某個button的click處理函數中。
private void Button_Click(object sender, RoutedEventArgs e)
{
// A specific node
HierarchyItem item = collection[0].Children[1].Children[1];
List<HierarchyItem> pendingSelectionPath = new List<HierarchyItem>();
while (item.Parent != null)
{
pendingSelectionPath.Insert(0, item.Parent);
item = item.Parent;
}
private void ExpandToPendingSelection(List<HierarchyItem> pendingSelectionPath)
{
// Grabs the treeview control.
var itemsControl = testTreeView as ItemsControl;
foreach (HierarchyItem item in pendingSelectionPath)
{
TreeViewItem container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if (container != null)
{
container.IsExpanded = true;
if (container.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
container.UpdateLayout();
}
itemsControl = container;
}
}
}
這樣就可以了,其實我不喜歡在blog里給出大量的代碼,原因我也說過。這次也是嘗試一下,如果有人喜歡,那么以后的技術類文章我也盡量給出一些代碼。其實這個問題還有一種更簡單的實現方式,提示一下,用IsExpanded這個DP來一個TwoWay的Binding。大家可以去試試看。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載