对 ObservableCollection 进行排序
假设我有员工类的 ObservableCollection
public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>();
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() {}
}
,现在我正在尝试对 ObservableCollection
(“employeeCollection”)进行排序 通过用户从组合框中进行适当的选择[它将是…按名字排序…按手机号码排序等…]..
并且需要返回排序的可观察集合…. 意味着它不应该采用“var”的形式,而应该是 ObservableCollection
所以我可以将它分配回 “ItemsControl”
的 “ItemsSource”
属性…
谢谢……
Suppose I have ObservableCollection
of employee class
public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>();
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() {}
}
now I am trying to sort the ObservableCollection
(“employeeCollection”)
by appropriate selection by user from combobox[it will be….Sort By FirstName….Sort By MobileNumber etc…]..
and it is required to get back sorted observable collection….
Means it should not be in form of “var” it should beObservableCollection<Employee>
So I can assign back it to “ItemsSource”
property of “ItemsControl”
…
Thanks……
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以对集合的视图进行排序,而不是对集合本身进行排序:
然后您可以使用 CollectionViewSource 作为 ItemSource:
You can sort the view of the collection rather that sorting the collection itself:
And then you can use the CollectionViewSource as ItemSource:
我认为 PVitt 可能有最好的解决方案...但是,我确实找到了这个
SortedObservableCollection 类也许可以提供帮助?
http://softcollections.codeplex.com/
I think PVitt may have the best solution... however, i did find this
SortedObservableCollection class that perhaps could help?
http://softcollections.codeplex.com/
我实现了一个
ObservableCollectionView
,它支持使用 lambda 进行排序和过滤(类似于 LINQ,但实时)和项目跟踪:https://mytoolkit.codeplex.com/wikipage?title=ObservableCollectionView
I implemented an
ObservableCollectionView
which supports sorting and filtering using a lambda (like LINQ but live) and item tracking:https://mytoolkit.codeplex.com/wikipage?title=ObservableCollectionView
您不需要自己排序,而是可以让 WPF 为您完成排序。例如,请参阅 SortDescription。
You don't need to sort yourself, but can let WPF do it for you. See SortDescription, for example.