为什么此CollectionViewSource不对枚举的ToString()结果排序? [英] Why does this CollectionViewSource not sort on the enum's ToString() result?
问题描述
我有一个视图模型,用于生成ItemsControl
的项目:
public class PermissionItemViewModel
{
public PermissionsEnum Permission {get; set;}
}
枚举本身的定义方式是,列表自然不是按字母顺序排列的,并且当控件呈现时,各项的顺序与枚举定义的顺序相同.
在我看来,我试图定义一个CollectionViewSource
并使它按本质上是myEnumValue.ToString()
的顺序对这些项目进行排序.取而代之的是,排序似乎是按枚举值的顺序进行的,而不是隐式的ToString()
结果.
<CollectionViewSource x:Key="permissionsViewSource"
Source="{Binding PermissionItems}"> <!-- ObservableCollection<PermissionItemViewModel> -->
<CollectionViewSource.SortDescriptions>
<cm:SortDescription PropertyName="Permission" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
最终,我使用这样的来源:
<ItemsControl ItemsSource="{Binding Source={StaticResource permissionsViewSource}}" />
我希望它按枚举的名称而不是它们的值进行排序.我该如何强迫它做到这一点?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CollectionViewSource source = (CollectionViewSource)(this.Resources["MyCollectionViewSource1"]);
ListCollectionView view = (ListCollectionView)source.View;
view.CustomSort = new CustomSorter();
}
public class CustomSorter : IComparer
{
public int Compare(object x, object y)
{
var itemx = x as PermissionItemViewModel;
var itemy = y as PermissionItemViewModel;
return $"{itemx.Permission}".CompareTo($"{itemy.Permission}");
}
}
您可以将其写为CollectionViewSource
的附带行为,以其属性值作为属性,以此来减轻您的良心.副手我想您想在View
上设置一个属性更改处理程序-我想我曾经写过这样的东西,让我们看看我是否可以挖掘它……
I have a view model that is used to produce items for an ItemsControl
:
public class PermissionItemViewModel
{
public PermissionsEnum Permission {get; set;}
}
The enum itself is defined in a way that the list is not naturally in alphabetical order, and when the control renders, the items are in the same order as the enum definition.
In my view, I am trying to define a CollectionViewSource
and make it sort those items by what would essentially be myEnumValue.ToString()
. Instead, the sort seems to be in order of the enum values instead of (what I thought would be) an implicit ToString()
result.
<CollectionViewSource x:Key="permissionsViewSource"
Source="{Binding PermissionItems}"> <!-- ObservableCollection<PermissionItemViewModel> -->
<CollectionViewSource.SortDescriptions>
<cm:SortDescription PropertyName="Permission" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
Ultimately, I use the source like this:
<ItemsControl ItemsSource="{Binding Source={StaticResource permissionsViewSource}}" />
I would like it to sort by the names of the enums, not their values. How can I force it to do that?
Looks like you may be able to grab the ListCollectionView
and do a custom sort on that.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CollectionViewSource source = (CollectionViewSource)(this.Resources["MyCollectionViewSource1"]);
ListCollectionView view = (ListCollectionView)source.View;
view.CustomSort = new CustomSorter();
}
public class CustomSorter : IComparer
{
public int Compare(object x, object y)
{
var itemx = x as PermissionItemViewModel;
var itemy = y as PermissionItemViewModel;
return $"{itemx.Permission}".CompareTo($"{itemy.Permission}");
}
}
You could salve your conscience a bit by writing this as an attached behavior for CollectionViewSource
that would take a property name for its value. Offhand I think you'd want that to set up a property changed handler on View
-- I think I wrote such a thing once, let's see if I can exhume it...
这篇关于为什么此CollectionViewSource不对枚举的ToString()结果排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!