【问题标题】:How to get Text from all Parent Nodes of a Node in TreeView?如何从 TreeView 中节点的所有父节点获取文本?
【发布时间】:2016-10-01 05:17:52
【问题描述】:

也许我不能很好地解释,但这应该解释一下: 我有一个名为 getParentNode(TreeNode) 的 int 字段来获取它有多少父节点(例如,如果节点下面有 2 个节点,则计数将为 2) 我有一个名为 getParentNames(TreeNode) 的列表字段,它返回所有父母的姓名。

getParentCount:

int getParentCount(TreeNode node)
{
    int count = 1;
    while (node.Parent != null)
    {
        count++;
        node = node.Parent;
    }
    return count;
}

getParentsNames:

List<string> getParentNames(TreeNode node)
{
    List<string> list = new List<string>();
    for (int i = 0; i < getParentCount(node); i++)
    {
        //i = 1 : list.Add(node.Parent.Text);
        //i = 2 : list.Add(node.Parent.Parent.Text);
        //i = 3 ...
    }
    return list;
}

我是否需要检查 (i == 0) (我不想手动编写,因为数字可以是任何东西)或什么? 问候。

【问题讨论】:

    标签: c# winforms treeview


    【解决方案1】:

    您可以使用以下任一选项:

    • 将节点的FullPath 拆分为树的PathSeparator
    • Ancestors 和 AncestorsAndSelf 扩展方法


    将节点的FullPath 拆分为树的PathSeparator

    您可以使用TreeNode 的FullPath 属性并使用TreeView 的PathSeparator 属性拆分结果。例如:

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        var ancestorsAndSelf = e.Node.FullPath.Split(treeView1.PathSeparator.ToCharArray());
    }
    

    Ancestors 和 AncestorsAndSelf 扩展方法

    您还可以获得TreeNode 的所有祖先。您可以简单地使用 while 循环来使用 node.Parent 而父级不为空。我更喜欢将此逻辑封装在扩展方法中,并使其在未来更易于重用。您可以创建一个扩展方法来返回一个节点的所有父节点(祖先):

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    public static class TreeViewExtensions
    {
        public static List<TreeNode> Ancestors(this TreeNode node)
        {
            return AncestorsInternal(node).Reverse().ToList();
        }
        public static List<TreeNode> AncestorsAndSelf(this TreeNode node)
        {
            return AncestorsInternal(node, true).Reverse().ToList();
        }
        private static IEnumerable<TreeNode> AncestorsInternal(TreeNode node, bool self=false)
        {
            if (self)
                yield return node;
            while (node.Parent != null)
            {
                node = node.Parent;
                yield return node;
            }
        }
    }
    

    用法:

    List<TreeNode> ancestors = treeView1.SelectedNode.Ancestors();
    

    您可以从祖先那里获取文本或任何其他属性:

    List<string> ancestors = treeView1.SelectedNode.Ancestors().Select(x=>x.Text).ToList(); 
    

    注意

    JFYI,您也可以使用扩展方法方法来获取所有子节点。在这里,我分享了一个扩展方法:Descendants Extension Method。

    【讨论】:

    • 哦,看起来不错。但我不明白可以从 treeView1.SelectedNode 调用异常方法。
    • 感谢您的反馈 :) - 扩展方法 实际上扩展了一个类型并为其添加新方法。在上述场景中,将TreeViewExtensions 添加到您的程序(在命名空间中)并在表单中使用该命名空间后,treeView1.SelectedNode 将有一个 Ancestors() 方法,您可以使用该方法获取节点的所有祖先。我使用相同的技术也得到了一个节点的所有descendants。
    • 向类型添加新的可重用功能确实是一种简洁优雅的机制。 (例如大多数 linq 方法是扩展方法,实际上并不存在于 List&lt;T&gt; 中,但是在使用 System.Linq 之后,它们将被添加到某些类型中,例如 List&lt;T&gt;。
    • 感谢您的解释:)
    【解决方案2】:

    为什么不使用node.FullPath 计算TreeView.PathSeparator 字符?类似的东西

    char ps = Convert.ToChar( TreeView1.PathSeparator);
    int nCount = selectedNode.FullPath.Split(ps).Length;
    

    【讨论】:

    • List lst = selectedNode.FullPath.Split(ps).ToList();
    【解决方案3】:

    无论如何,我注意到我需要使用 while 循环:

    List<string> getParentNames(TreeNode node)
        {
            List<string> list = new List<string>();
            int count = getParentCount(node);
            int index = 0;
            TreeNode parent = node;
            while (index < count)
            {
                if (parent != null)
                {
                    index++;
                    list.Add(parent.Text);
                    parent = parent.Parent;
                }
            }
            return list;
        }
    

    【讨论】:

    • Ancestors 扩展方法要好得多。它返回一个List&lt;Node&gt;,您可以简单地使用它从列表中获取节点的计数或任何属性,例如Text。
    • 可能你不知道扩展方法是什么或如何使用它。 Extension methods 使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但它们被调用时就好像它们是扩展类型上的实例方法一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多