如何按一个属性对CollectionViewSource进行排序,然后如何将另一个属性作为抢七排序? [英] How do you sort a CollectionViewSource by one property, then by another as a tiebreak?

查看:45
本文介绍了如何按一个属性对CollectionViewSource进行排序,然后如何将另一个属性作为抢七排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的CollectionViewSource按描述对项目集合进行排序.如果描述相同,则要基于ID进行排序.如何指定首先按描述排序,然后按ID排序?

Currently, my CollectionViewSource sorts a collection of items by description. If the description is the same, I want to sort based on ID. How can I specify to sort by description first, then by ID?

我尝试添加第二个带有PropertyName ="Id"的SortDescription,但这没有用.

I've tried adding a second SortDescription with PropertyName="Id", but that hasn't worked.

 <CollectionViewSource x:Key="Items" Source="{Binding Items}" >
 <CollectionViewSource.SortDescriptions>
 <scm:SortDescription PropertyName="Description"/>
 </CollectionViewSource.SortDescriptions>
 </CollectionViewSource>

编辑:ID属性在视图模型上是私有的.没有引发错误.

The ID property was private on the viewmodel. No errors thrown.

推荐答案

我不确定为什么为 Id 添加 SortDescription 不能正常工作,因为它应该可以正常工作

I'm not sure why adding the SortDescription for Id does not work as it should work fine.

赞:

<CollectionViewSource x:Key="Items" Source="{Binding ElementName=UI, Path=Items}" >
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Description" />
        <scm:SortDescription PropertyName="Id" />
    </CollectionViewSource.SortDescriptions>
 </CollectionViewSource>

我根据需要整理了一个完整的工作示例:

I put together a full example of this working as you want:

Xaml:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="MainWindow" Height="124" Width="464" Name="UI" >
<Window.Resources>

   <CollectionViewSource x:Key="Items" Source="{Binding ElementName=UI, Path=Items}" >
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Description" />
        <scm:SortDescription PropertyName="Id" />
    </CollectionViewSource.SortDescriptions>
   </CollectionViewSource>
</Window.Resources>

<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource Items}}" />
</Grid>

代码:

public partial class MainWindow : Window
{
    private ObservableCollection<MyObject> myVar = new ObservableCollection<MyObject>();

    public MainWindow()
    { 
        InitializeComponent();
        Items.Add(new MyObject { Description = "Stack", Id = 5 });
        Items.Add(new MyObject { Description = "OverFlow", Id = 1 });
        Items.Add(new MyObject { Description = "StackOverFlow", Id = 2 });
        Items.Add(new MyObject { Description = "Stack", Id = 1 });
        Items.Add(new MyObject { Description = "Stack", Id = 0 });
        Items.Add(new MyObject { Description = "OverFlow", Id = 7 });  
    }

    public ObservableCollection<MyObject> Items
    {
        get { return myVar; }
        set { myVar = value; }
    }
}


public class MyObject
{
    public int Id { get; set; }
    public string Description { get; set; }

    public override string ToString()
    {
        return string.Format("Desc: {0}, Id: {1}", Description, Id);
    }
}

结果:

这篇关于如何按一个属性对CollectionViewSource进行排序,然后如何将另一个属性作为抢七排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆