我试图找到一种在任何给定时间点在C#中获取WPF TreeView中的可见元素的好方法。例如,在这种情况下,我希望有:巴西,加拿大,小股票,Tarte au sucre,Tourtiere,Big stock,Denmark。
我的大多数研究都让我相信我需要遍历所有项目,看看它们是否可以单独看到。我有没有更好的内置方法?
谢谢!
答案 0 :(得分: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
}