欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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