如何从TreeView中的节点的所有父节点获取文本? [英] How to get Text from all Parent Nodes of a Node in TreeView?

查看:85
本文介绍了如何从TreeView中的节点的所有父节点获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Maybe I couldn't explain well, but this should explain: I have a int field called getParentNode(TreeNode) to get how many parent it has (e.g if there is 2 nodes below node, count will be 2) And I have a List field called getParentNames(TreeNode) that returns all of the parent's names.

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)(我不想手动写,因为数字可以是任何东西)或其他东西?问候.

Do I need to check if (i == 0) (I don't want to write manually because number can be anything) or something? Regards.

推荐答案

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

You can use either of these options:

  • 通过树的 PathSeparator 拆分节点的 FullPath
  • 祖先和AncestorsAndSelf扩展方法


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

You can use FullPath property of the TreeNode and split the result using PathSeparator property of TreeView. For example:

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

祖先和祖先和自扩展方法

您还可以获取 TreeNode 的所有祖先.您可以简单地使用while循环在父级不为null的情况下使用 node.Parent 向上跳转.我更喜欢将此逻辑封装在扩展方法中,并使其在将来可重用.您可以创建扩展方法以返回节点的所有父节点(祖先):

Ancestors and AncestorsAndSelf sxtension methods

Also you can get all ancestors of a TreeNode. You can simply use a while loop to go up using node.Parent while the parent is not null. I prefer to encapsulate this logic in an extension method and make it more reusable for future. You can create an extension method to return all parent nodes (ancestors) of a node:

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();

您可以从祖先那里获得文字或其他任何财产:

You can get text or any other property from ancestors:

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

注意

JFYI,您也可以使用扩展方法来获取所有子节点.在这里,我分享了一个扩展方法,以便于:后代扩展方法.

JFYI you can use an extension method approach to get all child nodes too. Here I've shared an extension method to to so: Descendants Extension Method.

这篇关于如何从TreeView中的节点的所有父节点获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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