对ObservableCollection进行排序
假设我有员工类的ObservableCollection
public ObservableCollection employeeCollection = new ObservableCollection(); public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public double MobileNumber { get; set; } public string City { get; set; } public int Age { get; set; } public Employee() {} }
现在我试图通过用户从combobox中进行适当选择来对ObservableCollection
(“employeeCollection”)进行排序[它将是…… .Sort By FirstName … .Sort By MobileNumber等…]
并且需要返回已排序的可观察集合…. 意味着它不应该是“var”的forms,它应该是ObservableCollection
所以我可以将它分配给“ItemsControl”
“ItemsSource”
属性……
谢谢……
您可以对集合的视图进行排序,而不是对集合本身进行排序:
// xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
然后您可以将CollectionViewSource用作ItemSource:
ItemsSource="{Binding Source={StaticResource ItemListViewSource}}"
我认为PVitt可能有最好的解决方案……但是,我确实找到了这个可能有帮助的SortedObservableCollection类?
我实现了一个ObservableCollectionView
,它支持使用lambda(如LINQ但是直播)和项目跟踪进行排序和过滤:
https://mytoolkit.codeplex.com/wikipage?title=ObservableCollectionView
你不需要自己排序,但可以让WPF为你做。 例如,请参阅SortDescription 。