排序的ObservableCollection [英] Sorting ObservableCollection
问题描述
假设我有的ObservableCollection
Employee类的
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() {}
}
现在我想排序的的ObservableCollection
(employeeCollection)
通过适当地选择从组合框用户[这将是... .Sort通过名字...... .Sort通过移动电话号码等..] ..
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…]..
和它需要的找回排序观察的集合......。
意味着它的不应该在VAR的形式,它应该是
的ObservableCollection&LT;员工&GT;
and it is required to get back sorted observable collection….
Means it should not be in form of "var" it should be
ObservableCollection<Employee>
所以,我可以分配回它的ItemsSource的
ItemsControl的
...
So I can assign back it to "ItemsSource"
property of "ItemsControl"
…
感谢......
推荐答案
您可以收集的意见进行排序,而该分类集合本身:
You can sort the view of the collection rather that sorting the collection itself:
// xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
<myView.Resources>
<CollectionViewSource x:Key="ItemListViewSource" Source="{Binding Itemlist}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="SortingProperty" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</myView.Resources>
然后就可以使用CollectionViewSource作为ItemSource:
And then you can use the CollectionViewSource as ItemSource:
ItemsSource="{Binding Source={StaticResource ItemListViewSource}}"
这篇关于排序的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!