2016-08-18 45 views
2

Example TreeView Structure獲取在WPF的TreeView

我試圖找到一個很好的方式來獲得在C#中的WPF TreeView的可視元素在任何給定時間點可見元素的列表。例如,在這種情況下,我預計會有:巴西,加拿大,小股票,Tarte au sucre,Tourtiere,Big stock,丹麥。

我的大部分研究都讓我相信我需要遍歷所有項目並查看它們是否單獨可見。有沒有更好的內置方法,我錯過了?

謝謝!

回答

1

據我所知,沒有一個「內置」的方法來檢查是否所有的父母和孩子都擴大了,只有在迭代後才能檢查他們的屬性。

這應該做的伎倆,讓所有的孩子在任何擴展父母,他們各自的父母添加到列表。

List<TreeViewItem> expandedTVI = new List<TreeViewItem>(); 
foreach (TreeViewItem item in treeView1.Items) 
{ 
    if (item.HasItems && item.IsExpanded) //if it has children, and the parent is expanded 
    { 
     foreach (TreeViewItem child in item.Items) 
      expandedTVI.Add(child); //add the child to the list 
    } 

    expandedTVI.Add(item); //always add the parent to the list 
}