通过比较 TreeView 节点的 Tag 属性对它们进行排序

发布于 2024-12-20 09:02:28 字数 156 浏览 3 评论 0原文

我编写了一个比较器,我想在 TreeView 的父节点上实现它,但我需要比较的是 Node.Tag.ToString() 而不是它们文本。据我所知,没有重载或其他函数可以使用 TreeView.Sort() 来执行此操作。

我想知道您是否有办法或解决方法?

I wrote a comparator and I want to implement it on parent nodes of a TreeView but what I need to be compared is the Node.Tag.ToString() and not by theire Text. As far as I looked there is no overload or another function to do this using TreeView.Sort().

I wonder if you have a way or workaround in mind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-12-27 09:02:28

如果您有 IComparer,看起来您可以设置 TreeViewNodeSorter 属性,然后调用 Sort 让节点按照您的意愿排序。

基于该链接的示例 - 未尝试,没有空检查等,但它应该可以工作:

public class NodeSorter : IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode tx = x as TreeNode;
        TreeNode ty = y as TreeNode;
        return string.Compare(tx.Tag.ToString(), ty.Tag.ToString());
    }
}

然后在您的设置中的某处 treeView1.TreeViewNodeSorter = new NodeSorter();

If you have an IComparer, it looks like you can set the TreeViewNodeSorter property to it and then call Sort to have the nodes sorted as you wish.

Example based on that link - not tried, no null checks etc. but it should work:

public class NodeSorter : IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode tx = x as TreeNode;
        TreeNode ty = y as TreeNode;
        return string.Compare(tx.Tag.ToString(), ty.Tag.ToString());
    }
}

Then treeView1.TreeViewNodeSorter = new NodeSorter(); somewhere in your setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文