递归折叠自定义UserControl中父节点的所有子节点 [英] Recursively collapse all child nodes of parent node in custom UserControl

查看:53
本文介绍了递归折叠自定义UserControl中父节点的所有子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上有一个类似于 TreeView 的自定义 UserControl.它唯一缺少的是 TreeView 中的 CollapseAll() 方法,所以我希望 a) 找到 CollapseAll 方法的源代码以便我可以复制它,或者 b) 手动(并递归地)遍历选定节点的所有子节点并折叠它们.

I actually have a custom-built UserControl similar to a TreeView. The only thing it's missing is the CollapseAll() method from a TreeView, so I'm looking to either a) find the source code for the CollapseAll method so I can copy it, or b) manually (and recursively) iterate through all the child nodes of a selected node and collapse them.

这是我目前的折叠方法:

Here is my current collapse method:

private void OnNodeCollapsed(TreeNode node) {
    foreach(FileSystemNode n in node.Nodes) {
        if(n.NodeType != FileSystemNodeType.Computer && n.NodeType != FileSystemNodeType.Drive) {
            if(n.Nodes.Count > 0) {
                n.Nodes.Clear();
            }
        }
    }

    foreach(TreeNode child in node.Nodes) {
        // and this is where I lose it
    }
}

我只想折叠子节点,而不是整棵树.有没有办法做到这一点?

I only want to collapse child nodes, not the whole tree. Is there a way of doing that?

一个很好的解决方案是这样的:带收益率的递归元素在树中的顺序.

推荐答案

您可以为自定义控件添加这样的扩展,并使用以下展开/折叠方法

You could add an extension like this for your Custom Control, and make use of the following Expand/Collapse Method

public static class TreeViewExt
{
     public static void ExpandRecursively(this ItemsControl itemsControl, bool expand, int levelDepth)
    {
        int depth = levelDepth == int.MaxValue ? levelDepth : levelDepth - 1;
       TreeViewItem treeViewItem = itemsControl as TreeViewItem;
        if (treeViewItem != null)
            treeViewItem.IsExpanded = expand || levelDepth >= 0; // expand, or keep expanded when levelDepth >= 0
         if (levelDepth > 0 || !expand)
        {
            // get container generator of itemsControl
            ItemContainerGenerator itemContainerGenerator = itemsControl.ItemContainerGenerator;
             // if containers have already been generated, the subItems can be expanded/collapsed
            if (itemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
            {
                for (int i = itemsControl.Items.Count - 1; i >= 0; --i)
                {
                    ItemsControl childControl = itemContainerGenerator.ContainerFromIndex(i) as ItemsControl;
                    if (childControl != null)
                        childControl.ExpandRecursively(expand, depth);
                }
            } 
            else
            {
                EventHandler handler = null; // store in variable, so the handler can be detached
                handler = new EventHandler((s, e) =>
                {
                    if (itemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                    {
                        for (int i = itemsControl.Items.Count - 1; i >= 0; --i)
                        {
                            ItemsControl childControl = itemContainerGenerator.ContainerFromIndex(i) as ItemsControl;
                            if (childControl != null)
                                childControl.ExpandRecursively(expand, depth);
                            itemContainerGenerator.StatusChanged -= handler; // detach
                        }
                    }
                });
                itemContainerGenerator.StatusChanged += handler; // attach
            }
        }
    }
}

完整代码:Treeview 递归展开/折叠

由于 OP 想为 WinForm UserControl 执行此操作,您可以使用这样的方法循环遍历子控件,

Since OP wanted to do it for WinForm UserControl, You could loop through the Child Controls using the method like this,

private IEnumerable<Node> getAllNodesRecursively(Node subnode) 
{ 
    // Return the parent before its children
    yield return subnode; 

    foreach (Node node in subnode.Nodes) 
    {
        foreach(Node n in getAllNodesRecursively(node))
        {
            yield return n;
        }
    }
} 

在树中以yield返回元素顺序的递归

这篇关于递归折叠自定义UserControl中父节点的所有子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆