如何在 Treeview 控件中使子节点可见 = false [英] How to make a child node visible = false in a Treeview Control

查看:24
本文介绍了如何在 Treeview 控件中使子节点可见 = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有树视图控件的 Windows 窗体.这个树视图有一个根节点和 2 个子节点.我的要求是我需要隐藏第一个子节点.是否有可能使特定孩子点头可见是错误的

I'm having a windows form with a tree view control. This tree view has a Root node and 2 child nodes. My requirement is i need to hide the first child node. Is it possible to make visible false that particular child nod

推荐答案

是的,您可以从树节点继承并创建自己的行为.像这样.

Yes you could inherit from tree node and create your own behaviour. Like so.

public class RootNode : TreeNode
{
    public List<ChildNode> ChildNodes { get; set; }

    public RootNode()
    {
        ChildNodes = new List<ChildNode>();
    }

    public void PopulateChildren()
    {
        this.Nodes.Clear();

        var visibleNodes = 
            ChildNodes
            .Where(x => x.Visible)
            .ToArray();

        this.Nodes.AddRange(visibleNodes);
    }

    //you would use this instead of (Nodes.Add)
    public void AddNode(ChildNode node)
    {
        if (!ChildNodes.Contains(node))
        {
            node.ParentNode = this;
            ChildNodes.Add(node);
            PopulateChildren();
        }
    }

    //you would use this instead of (Nodes.Remove)
    public void RemoveNode(ChildNode node)
    {
        if (ChildNodes.Contains(node))
        {
            node.ParentNode = null;
            ChildNodes.Remove(node);
            PopulateChildren();
        }

    }
}

public class ChildNode : TreeNode
{
    public RootNode ParentNode { get; set; }
    private bool visible;
    public bool Visible { get { return visible; } set { visible = value;OnVisibleChanged(): } }
    private void OnVisibleChanged()
    {
        if (ParentNode != null)
        {
            ParentNode.PopulateChildren();
        }
    }
}

这篇关于如何在 Treeview 控件中使子节点可见 = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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