有没有一种方法可以根据Excel的字符串排序逻辑升序对字符串进行排序,而无需编写自定义代码?

问题描述 投票:0回答:1

我有一个 Infragistics XamComboEditor,它需要根据 Excel 的升序排序逻辑对其值进行排序。它绑定到一个 ObservableDictionary,并以 DisplayMemberPath 作为值。该值可以同时包含特殊字符、数字和字母,也可以仅包含其中之一。有没有什么方法可以实现这个需求,而不需要手动处理所有情况?

我尝试过使用ChatGPT,但它提供的代码没有提供预期的结果。 This is a sample input in excel Sorting it in ascending order in Excel givs following result

c# wpf wpf-controls infragistics .net-4.8
1个回答
0
投票

在代码中定义一个属性(可能是查看模型):

ICollectionView Source { get; set; }

在代码中的某个位置创建一个默认的 Collection View,它实际上维护一堆 SortDescription 对象,每个对象都是一个可以保存列信息和排序方向的结构 - https://learn.microsoft.com/ en-us/dotnet/desktop/wpf/data/how-to-get-the-default-view-of-a-data-collection?view=netframeworkdesktop-4.8

你的控件的项目源应该这样设置:

YourControl.ItemsSource = CollectionViewSource.GetDefaultView(this.Source);

最后,您可以轻松地应用排序:

Source.SortDescriptions.Add(new SortDescription("Name",SortDirection.Descending));

按“名称”顺序排列。

© www.soinside.com 2019 - 2024. All rights reserved.