如何将 WPF TreeView 的所有元素作为列表获取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13379058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all the elements of a WPF TreeView as a List?
提问by Ignacio Soler Garcia
I need to access the nodes of a TreeView as a plain list (as if all the nodes where expanded) to be able to do multiselection pressing the Shift key. Is there a way to accomplish this?
我需要访问 TreeView 的节点作为一个普通列表(就像所有节点都展开了一样),以便能够按 Shift 键进行多选。有没有办法做到这一点?
Thanks
谢谢
回答by Sisyphe
Here is a method that will retrieve all the TreeViewItems in a TreeView. Please be aware that this is an extremely expensive method to run, as it will have to expand all TreeViewItems nodes and perform an updateLayout each time. As the TreeViewItems are only created when expanding the parent node, there is no other way to do that.
这是一个将检索 TreeView 中所有 TreeViewItems 的方法。请注意,这是一种运行起来非常昂贵的方法,因为它每次都必须展开所有 TreeViewItems 节点并执行 updateLayout。由于 TreeViewItems 仅在展开父节点时创建,因此没有其他方法可以做到这一点。
If you only need the list of the nodes that are already opened, you can remove the code that expand them, it will then be much cheaper.
如果你只需要已经打开的节点列表,你可以去掉展开它们的代码,这样会便宜很多。
Maybe you should try to find another way to manage multiselection. Having said that, here is the method :
也许您应该尝试寻找另一种管理多选的方法。话虽如此,这里是方法:
public static List<TreeViewItem> FindTreeViewItems(this Visual @this)
{
if (@this == null)
return null;
var result = new List<TreeViewItem>();
var frameworkElement = @this as FrameworkElement;
if (frameworkElement != null)
{
frameworkElement.ApplyTemplate();
}
Visual child = null;
for (int i = 0, count = VisualTreeHelper.GetChildrenCount(@this); i < count; i++)
{
child = VisualTreeHelper.GetChild(@this, i) as Visual;
var treeViewItem = child as TreeViewItem;
if (treeViewItem != null)
{
result.Add(treeViewItem);
if (!treeViewItem.IsExpanded)
{
treeViewItem.IsExpanded = true;
treeViewItem.UpdateLayout();
}
}
foreach (var childTreeViewItem in FindTreeViewItems(child))
{
result.Add(childTreeViewItem);
}
}
return result;
}
回答by user1480312
Here is what you asked;
这是你问的;
private static TreeViewItem[] getTreeViewItems(TreeView treeView)
{
List<TreeViewItem> returnItems = new List<TreeViewItem>();
for (int x = 0; x < treeView.Items.Count; x++)
{
returnItems.AddRange(getTreeViewItems((TreeViewItem)treeView.Items[x]));
}
return returnItems.ToArray();
}
private static TreeViewItem[] getTreeViewItems(TreeViewItem currentTreeViewItem)
{
List<TreeViewItem> returnItems = new List<TreeViewItem>();
returnItems.Add(currentTreeViewItem);
for (int x = 0; x < currentTreeViewItem.Items.Count; x++)
{
returnItems.AddRange(getTreeViewItems((TreeViewItem)currentTreeViewItem.Items[x]));
}
return returnItems.ToArray();
}
Call with your control as the first parameter e.g.;
将您的控件作为第一个参数调用,例如;
getTreeViewItems(treeView1);

