gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:09:40 26 4
gpt4 key购买 nike

我有一个带有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com