这是我的代码:
public Form1()
{
InitializeComponent();
treeView1.Nodes.Add(new TreeNode("Graphic Requests"));
if (TreeNodesList == null) TreeNodesList = new List<TreeNode>();
TreeNodesList.Add(new TreeNode("Art Not Started"));
TreeNodesList.Add(new TreeNode("Art In Progress"));
TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
TreeNodesList.Add(new TreeNode("Final Art Not Locked"));
foreach (var node in TreeNodesList)
{
treeView1.Nodes.Add(node);
}
}
我期望有一个父节点“ Graphic Requests”,其余的节点作为子节点添加。但是,我的列表如下所示:
有人可以告诉我我所缺少的吗?
最佳答案
您正在将节点添加到treeView1中,这就是为什么它们成为父节点而不是子节点的原因。而是将它们添加到您添加的第一个节点。
尝试这个:
public Form1()
{
InitializeComponent();
var parent = new TreeNode("Graphic Requests");
TreeNodesList.Add(new TreeNode("Art Not Started"));
TreeNodesList.Add(new TreeNode("Art In Progress"));
TreeNodesList.Add(new TreeNode("Items To Accept/Modify"));
TreeNodesList.Add(new TreeNode("Final Art Not Locked"));
foreach (var node in TreeNodesList)
{
parent.Nodes.Add(node);
}
treeView1.Nodes.Add(parent);
}