c# - 如何在 Treeview 控件中使子节点可见 = false

标签 c# winforms treeview

我有一个带有 TreeView 控件的 Windows 窗体。此 TreeView 有一个根节点和 2 个子节点。我的要求是我需要隐藏第一个子节点。 是否有可能使特定 child 点头的可见错误

最佳答案

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

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

关于c# - 如何在 Treeview 控件中使子节点可见 = false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253244/

相关文章:

java - 需要显示一个可以动态(无限)添加到(java)的树状结构?

c# - 创建一个函数,它给我们另一个函数

c# - 定期更新屏幕数据的模式

c# - 如何使用 C# 或 VB.NET 获取当前本地主机名?

c# - 在 WinForms 中保留设计时属性

c# - 使用 linq 按字母顺序对 TreeView 进行排序

python - 将不同长度的列添加到 gtk TreeStore(Treeview)

c# - IEnumerable<T>.ToLookup<TKey, TValue>

c# - 如何分割特定于文化的字符串

c# - 向 Word 添加自定义任务 Pane (不使用 VSTO)