列表框中的数字字符串排序

发布于 2024-12-20 23:47:10 字数 645 浏览 1 评论 0原文

我有一个 ListBox,其中 ItemSource 绑定到 CollectionViewSource。 CVS 源是一个XmlDataProvider。所以列表框列出了我指定的所有节点(名称属性)。 现在这些节点具有属性,我希望 ListBox 按它们排序。 问题是,由于底层数据是xml,所以每个值(节点的属性)都是一个字符串,但有些值代表数值。由于使用 CollectionViewSource.SortDescriptions.add (...) 排序会按字母顺序对这些(字符串)值进行排序,因此 2,10,5 的序列将被排序为 10,2,5 而不是 2, 5,10。 我该如何解决这个问题?

如果解决方案在于 ListView 的 CustomSort,有人可以为我提供一个关于如何使用底层 XmlDocument 实现此目的的快速示例吗?

我认为这就像编写一个实现 IComparer 的类一样简单,但不知何故我迷失了。 我想将属性的名称传递给方法,这样我就可以从 CVS 中“提取”所有这些属性,将它们转换为浮点数(在本例中)并使用标准函数对它们进行排序... 但说实话,我完全不知道 CustomSort 是如何工作的......

希望这在不放弃 XmlDocument 的情况下是可能的,因为它是一种给定的:)

问候

I have a ListBox which ItemSource is bound to a CollectionViewSource. The CVS Source is an XmlDataProvider. So the ListBox lists all nodes (name attribute) i specified.
Now those nodes have attributes, and i want the ListBox to be sorted by them.
The problem is, since the underlying data is xml, every value(attributes of the node) is a string, but some of the values represent numerical values. Since sorting with CollectionViewSource.SortDescriptions.add (...) will sort those (string)values alphabetically, a sequence of 2,10,5 will be sorted as 10,2,5 instead of 2,5,10.
How can i solve this?

If the solution lies in the ListView's CustomSort, could someone please provide me a quick example on how to to this with underlying XmlDocument?

I thought it would be as easy as writing a class which implements IComparer, but somehow i am lost.
I wanted to pass the name of the attribute to the method, so i could just "extract" all those attributes from the CVS, convert them to float (in this case) and sort them with standard functions...
But i am totally lost on how this CustomSort works to be honest....

Hope this is possible without ditching the XmlDocument, because it is kind of a given :)

Regards

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

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

发布评论

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

评论(1

缘字诀 2024-12-27 23:47:10

如果要绑定到从 IList 继承的集合,则可以从 ListView 控件的 ItemsSource 属性检索 ListCollectionView。拥有 ListCollectionView 的实例后,您可以将排序方法分配给 CustomSorter 属性。

自定义排序器必须继承旧式的非泛型 IComparer。在 Compare 方法中,您将获得绑定类的两个实例。您可以根据需要投射这些以获得您想要的结果。在开发过程中,您可以将调试器锚定在 Compare 方法内,以确定对象到底是什么。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> strings = new List<string>() { "3", "2", "10", "1" };
        lv1.ItemsSource = strings;
        ListCollectionView lcv = 
             CollectionViewSource.GetDefaultView(lv1.ItemsSource) as ListCollectionView;
        if(lcv!=null)
        {
            lcv.CustomSort = new MySorter();
        }
    }
}
public class MySorter : IComparer
{
    public int Compare(object x, object y)
    { // set break point here!
        return Convert.ToInt32(x) - Convert.ToInt32(y);
    }
}

If you are binding to a collection that inherits from IList, you can retrieve a ListCollectionView from the ItemsSource property of your ListView control. Once you have an instance of a ListCollectionView, you can assign a sorting method to the CustomSorter property.

The custom sorter must inherit from the old style, non-generic IComparer. Within the Compare method, you get two instances of the bound class. You can cast those as needed to get your desired result. During development, you can anchor the debugger inside the Compare method to determine exactly what the objects are.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> strings = new List<string>() { "3", "2", "10", "1" };
        lv1.ItemsSource = strings;
        ListCollectionView lcv = 
             CollectionViewSource.GetDefaultView(lv1.ItemsSource) as ListCollectionView;
        if(lcv!=null)
        {
            lcv.CustomSort = new MySorter();
        }
    }
}
public class MySorter : IComparer
{
    public int Compare(object x, object y)
    { // set break point here!
        return Convert.ToInt32(x) - Convert.ToInt32(y);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文