WPF TreeView控件子节点的展开与收缩
程序员文章站
2022-06-08 13:55:25
...
WPF TreeView控件子节点的展开与收缩
一、节点全展开
foreach (var item in treeView.Items)
{
DependencyObject dObject = treeView.ItemContainerGenerator.ContainerFromItem(item);
((TreeViewItem)dObject).ExpandSubtree();
}
二、节点全收缩
foreach (var item in treeView.Items)
{
DependencyObject dObject = treeView.ItemContainerGenerator.ContainerFromItem(item);
CollapseTreeviewItems(((TreeViewItem)dObject));
}
private void CollapseTreeviewItems(TreeViewItem Item)
{
Item.IsExpanded = false;
foreach (var item in Item.Items)
{
DependencyObject dObject = Item.ItemContainerGenerator.ContainerFromItem(item);
if (dObject != null)
{
((TreeViewItem)dObject).IsExpanded = false;
if (((TreeViewItem)dObject).HasItems)
{
CollapseTreeviewItems(((TreeViewItem)dObject));
}
}
}
}
上一篇: WPF如何实现TreeView节点重命名