I have a ObservableCollection , where MyData is a class with 4 properties i.e. int id, string name, bool IsSelected, string IsVisible.
我有一个ObservableCollection,其中MyData是一个具有4个属性的类,即int id,string name,bool IsSelected,string IsVisible。
This ObservableCollection is binded to a combobox with checkboxes(Cities data for example). Now, when the user checks the checkboxes then next time when he opens the drop down - all selections should come on top in ascending order by name.
此ObservableCollection绑定到带有复选框的组合框(例如,城市数据)。现在,当用户选中复选框时,下次打开下拉菜单时 - 所有选项都应按名称按升序排在最前面。
I have also implemented auto complete when the user types in 3 chars in the combobox, the drop down will open showing all the selections first, then then all the items starting from the 3 chars type in by the user.
当用户在组合框中键入3个字符时,我也实现了自动完成,下拉将打开,首先显示所有选择,然后由3个字符开始的所有项目由用户键入。
I have researched and implemented the following code and it is working fine, but i want to know whether this is the best approach or can i implement this in a better manner, code is :
我已经研究并实现了以下代码,它工作正常,但我想知道这是否是最好的方法,还是我能以更好的方式实现它,代码是:
IEnumerable<MyData> sort;
ObservableCollection<MyData> tempSortedCities = new ObservableCollection<MyData>();
sort = City.OrderByDescending(item => item.IsSelected).ThenBy(item => item.Name.ToUpper()) ;
// City is my observablecollection<MyData> property in my Model binded to combobox in UI
foreach (var item in sort)
tempSortedCities.Add(item);
City.Clear(); // City is my observablecollection<MyData> property in my Model
City = tempSortedCities;
tempSortedCities = null;
sort = null;
Thanks in advance for your time !
在此先感谢您的时间 !
1 个解决方案
#1
23
ICollectionView
seems to be a perfect fit for this. It was designed specifically for sorting, filtering and grouping of a collection without modifying the original collection.
ICollectionView似乎非常适合这种情况。它专门用于对集合进行排序,过滤和分组,而无需修改原始集合。
You can get an instance of ICollectionView
for your collection using the following code:
您可以使用以下代码获取集合的ICollectionView实例:
var sortedCities = CollectionViewSource.GetDefaultView(City);
Then you can setup sorting by adding instances of SortDescription
type to the ICollectionView.SortDescriptions
collection:
然后,您可以通过将SortDescription类型的实例添加到ICollectionView.SortDescriptions集合来设置排序:
sortedCities.SortDescriptions.Add(new SortDescription("IsSelected", ListSortDirection.Descending));
sortedCities.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
Then you can bind your ComboBox
directly to the collection view (instead of City
collection) and it will display already sorted data.
然后,您可以将ComboBox直接绑定到集合视图(而不是City集合),它将显示已排序的数据。
#1
23
ICollectionView
seems to be a perfect fit for this. It was designed specifically for sorting, filtering and grouping of a collection without modifying the original collection.
ICollectionView似乎非常适合这种情况。它专门用于对集合进行排序,过滤和分组,而无需修改原始集合。
You can get an instance of ICollectionView
for your collection using the following code:
您可以使用以下代码获取集合的ICollectionView实例:
var sortedCities = CollectionViewSource.GetDefaultView(City);
Then you can setup sorting by adding instances of SortDescription
type to the ICollectionView.SortDescriptions
collection:
然后,您可以通过将SortDescription类型的实例添加到ICollectionView.SortDescriptions集合来设置排序:
sortedCities.SortDescriptions.Add(new SortDescription("IsSelected", ListSortDirection.Descending));
sortedCities.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
Then you can bind your ComboBox
directly to the collection view (instead of City
collection) and it will display already sorted data.
然后,您可以将ComboBox直接绑定到集合视图(而不是City集合),它将显示已排序的数据。