仅在填充后重新排序树视图节点 [0]
我有以下树:
Animals
|
|___Zebra
| |__Head
| |__Arms
| |__Legs
|
|___Monkey
|__Head
|__Arms
|__Legs
每个动物都有一个 ID 号存储在“标签”字段中,它们的名称位于节点的“名称”字段中。我想按一个显示“按 ID 排序”的按钮,并将上面的“斑马”变成“ 14" 等,然后按数字排序。然而,我希望孩子们保持头、手臂、腿的顺序。当我使用下面的代码时,它可以工作,但它也会将头臂腿重新排序为臂、头、腿。我尝试过 NodeSorter,但没有得到任何不同的结果。我对 C# 也很陌生,所以我可能错误地实现了它。 :) 我还使用带有一些额外字段的自定义节点来存储数据和布尔值。这就是下面所指的“JacksNode”。
这是代码:
public static void sortByAnimalID(TreeView tv)
{
tv.BeginUpdate();
foreach (TreeNode treeNode in tv.Nodes[0].Nodes)
{
if (((JacksNode)treeNode).IsAnimal)
{
treeNode.Text = Convert.ToString(treeNode.Tag);
treeNode.Name = Convert.ToString(treeNode.Tag);
}
}
tv.Sort();
tv.EndUpdate();
}
对我做错了什么有什么想法吗?我已经在网上搜索了两周,所有的树视图文章都让我不知所措。然而,没有一个如此具体。 感谢伙计们/女士们的任何建议。
I have the following tree:
Animals
|
|___Zebra
| |__Head
| |__Arms
| |__Legs
|
|___Monkey
|__Head
|__Arms
|__Legs
Each animal has an ID number stored in the Tag field and their name is in Name field of node.I want to press a button that says "Sort by ID" and have the "Zebra" above turn into "14" etc, and then resort numerically. However, I want the children to stay with the head, arms, legs order. When I use the following code, it works, but it also re-sorts the head arms legs into arms, head, legs. I've tried a NodeSorter, but I just didn't get any different results. I'm also very new to C# so I might have implemented it incorrectly. :) I'm also using a custom node with a few extra fields to store data and boolean values. That's what the "JacksNode" refers to below.
Here's the code:
public static void sortByAnimalID(TreeView tv)
{
tv.BeginUpdate();
foreach (TreeNode treeNode in tv.Nodes[0].Nodes)
{
if (((JacksNode)treeNode).IsAnimal)
{
treeNode.Text = Convert.ToString(treeNode.Tag);
treeNode.Name = Convert.ToString(treeNode.Tag);
}
}
tv.Sort();
tv.EndUpdate();
}
Any ideas on what I'm doing wrong? I've searched the web for two weeks and have been overwhelmed with all the treeview articles. However, none have been this specific.
Thanks guys/gals for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 TreeNode.Level 属性来了解如何比较节点属性。像这样:
Use the TreeNode.Level property to figure out how to compare node properties. Like this: