1. 程式人生 > >WPF TreeView如何展開到某個節點

WPF TreeView如何展開到某個節點

初用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

{

}

  然後準備資料來源,並繫結到你的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 pendingSelectionPath = new List();

while (item.Parent != null)

{

pendingSelectionPath.Insert(0, item.Parent);

item = item.Parent;

}

private void ExpandToPendingSelection(List 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。大家可以去試試看。
轉載自:https://www.evget.com/article/2011/3/1/15880.html