要在Winform Tree中添加自定義節點,可以創建一個自定義類來表示節點,然后將這些自定義節點添加到Tree中。
首先,創建一個自定義類來表示節點,例如:
public class CustomNode
{
public string Text { get; set; }
public List<CustomNode> Children { get; set; }
public CustomNode(string text)
{
Text = text;
Children = new List<CustomNode>();
}
}
然后,在Winform窗體中添加一個Tree控件,并在代碼中創建一個根節點和一些自定義節點,并將它們添加到Tree中,例如:
private void Form1_Load(object sender, EventArgs e)
{
CustomNode rootNode = new CustomNode("Root");
CustomNode childNode1 = new CustomNode("Child 1");
CustomNode childNode2 = new CustomNode("Child 2");
rootNode.Children.Add(childNode1);
rootNode.Children.Add(childNode2);
AddNodesToTree(treeView1.Nodes, rootNode);
}
private void AddNodesToTree(TreeNodeCollection nodes, CustomNode customNode)
{
TreeNode treeNode = new TreeNode(customNode.Text);
foreach (CustomNode childNode in customNode.Children)
{
AddNodesToTree(treeNode.Nodes, childNode);
}
nodes.Add(treeNode);
}
通過以上代碼,可以在Winform Tree中添加自定義節點。在加載窗體時創建自定義節點,并通過遞歸方法將節點添加到Tree中。