• WPF ListCollectionView排序、过滤和分组


    我之前写过我非常需要并且非常喜欢 WPF 列表(ListBox、ListView..)。现在我想分享一种方法来在简单地绑定到集合而不触及该集合时操作视图。它可以在 xaml 中完成,如此处所示但我不太喜欢这种方法,因为在我看来,排序、分组等操作属于视图模型,而不是直接视图,如果它们在视图中,则没有可用的测试。

    例子

    我们有一个 ListBox 视图,其中 ItemsSource 绑定到 ObservableCollection。在我的示例中,我有一个 True Blood 角色列表。每个生物都有名字、姓氏、年龄(组成)和种类(可以是吸血鬼、人类或神秘人物)。一个生物模型是(很简单,只是有一些数据):

    1. public class CreatureModel
    2. {
    3. public CreatureModel( string firstName, string lastName, int age,
    4. CreatureKind kind )
    5. {
    6. this.FirstName = firstName;
    7. this.LastName = lastName;
    8. this.Age = age;
    9. this.Kind = kind;
    10. }
    11. public string FirstName { get; set; }
    12. public string LastName { get; set; }
    13. public int Age { get; set; }
    14. public CreatureKind Kind { get; set; }
    15. public override string ToString()
    16. {
    17. return string.Format( "{0} {1} {2} {3}", this.FirstName,
    18. this.LastName, this.Age, this.Kind );
    19. }
    20. }

    View 包含一个 ListBox 和三个按钮:

    1. <ListBox ItemsSource="{Binding Path=CreaturesCollection}" />
    2. <Button Content="Sort by age"
    3. Command="{Binding Path=SortByAgeCommand}"/>
    4. <Button Content="Filter Vampires"
    5. Command="{Binding Path=FilterVampiresCommand}"/>
    6. <Button Content="Group by kind"
    7. Command="{Binding Path=GroupByKindCommand}"/>

    这个问题的视图模型也很简单——我们需要一组生物和命令来进行排序、过滤和分组:

    1. public ObservableCollection CreaturesCollection
    2. { get; private set; }
    3. public ICommand SortByAgeCommand
    4. { get { return new DelegateCommand(this.SortByAge ); } }
    5. public ICommand FilterVampiresCommand
    6. { get { return new DelegateCommand( this.FilterVampires ); } }
    7. public ICommand GroupByKindCommand
    8. { get { return new DelegateCommand(this.GroupByKind ); } }

    现在实现三个方法所需的所有魔法——DelegateCommand 构造函数中的参数就是这个方法:

    1. private ListCollectionView GetListCollectionView()
    2. {
    3. return (ListCollectionView) CollectionViewSource
    4. .GetDefaultView( this.CreaturesCollection );
    5. }

    查看集合源

    CollectionViewSource – 当您将绑定设置为集合时,WPF 会将其绑定到该集合的默认视图。CollectionViewSource 有一个静态方法可以让您获取此默认视图:CollectionViewSource.GetDefaultView 并且可以轻松应用视图过滤、分组、排序 ListCollectionView 是实现 IList 的集合的默认视图。

    排序 – ListCollectionView.CustomSort

    要应用排序,需要 IComperer 的实现,它可以是此接口的任何实现。我将使用一种简单地按年龄对生物进行分类的

    1. public class SortCreaturesByAge : IComparer
    2. {
    3. public int Compare( object x, object y )
    4. {
    5. if( x as CreatureModel == null && y as CreatureModel == null )
    6. {
    7. throw new ArgumentException( "SortCreatures can
    8. only sort CreatureModel objects." );
    9. }
    10. if( ((CreatureModel) x).Age > ((CreatureModel) y).Age )
    11. {
    12. return 1;
    13. }
    14. return -1;
    15. }
    16. }

    现在我可以通过在 ListCollectionView 上设置 CustomSort 来实现用于 SortByAgeCommand 的方法:

    1. public ICommand SortByAgeCommand
    2. { get { return new DelegateCommand(this.SortByAge ); } }
    3. private void SortByAge()
    4. {
    5. this.GetListCollectionView().CustomSort = new SortCreaturesByAge();
    6. }

    未排序和排序的集合:

    过滤——ListCollectionView。筛选

    我想实现一个过滤器,它只显示生物列表中的吸血鬼。过滤器是一个Predicate。甚至比排序更简单,我们只需要
    一个返回 bool 的方法来判断参数对象是否是我们需要的:

    1. private bool IsAVampire( object obj )
    2. {
    3. if(obj as CreatureModel != null
    4. && (obj as CreatureModel).Kind == CreatureKind.Vampire)
    5. {
    6. return true;
    7. }
    8. return false;
    9. }


    点击“过滤”

    分组

    我想按种类对列表中的所有生物进行分组。Kind 是一个枚举:

    1. public enum CreatureKind
    2. {
    3. Human,
    4. Vampire,
    5. Mystery
    6. }

    ListCollectionView 再次提供了一个解决方案。所需要做的就是添加一个分组所基于的属性名称。我这个例子 - 种类:

    1. public ICommand GroupByKindCommand
    2. { get { return new DelegateCommand(this.GroupByKind ); } }
    3. private void GroupByKind()
    4. {
    5. this.GetListCollectionView().GroupDescriptions.Add(
    6. new PropertyGroupDescription { PropertyName = "Kind" } );
    7. }


    现在可以对列表进行分组:

    总结

    WPF 很好地支持分组、过滤和排序,并且易于应用。我觉得我的示例非常基础,并且像往常一样在 WPF 中可以在这里完成很多自定义。接下来我计划发现一种巧妙而巧妙的方法来为这类问题设置数据模板和样式。

  • 相关阅读:
    CSS实现白天/夜晚模式切换
    拒绝内卷,阿里架构师整理的这份Java核心手册,堪称最强
    数字世界的探索者:计算机相关专业电影精选推荐
    LeetCode42:接雨水
    一文详解Linux内核数据结构之kfifo
    业务及系统架构对发布的影响
    Doris安装部署
    python遗传算法(应用篇1)--求解一元函数极值
    JavaWeb之jQuery
    Spring(01)
  • 原文地址:https://blog.csdn.net/liaohaiyin/article/details/126003234