C# 您如何按一个属性对 CollectionViewSource 进行排序,然后再按另一个属性排序作为决胜局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14992072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you sort a CollectionViewSource by one property, then by another as a tiebreak?
提问by Khelvaster
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?
目前,我的 CollectionViewSource 按描述对项目集合进行排序。如果描述相同,我想根据 ID 进行排序。如何指定先按描述排序,然后按 ID 排序?
I've tried adding a second SortDescription with PropertyName="Id", but that hasn't worked.
我试过用 PropertyName="Id" 添加第二个 SortDescription,但这没有用。
<CollectionViewSource x:Key="Items" Source="{Binding Items}" >
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
EDIT:The ID property was private on the viewmodel. No errors thrown.
编辑:ID 属性在视图模型上是私有的。没有抛出错误。
采纳答案by sa_ddam213
I'm not sure why adding the SortDescription
for Id
does not work as it should work fine.
我不确定为什么添加SortDescription
forId
不起作用,因为它应该可以正常工作。
Like this:
像这样:
<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:
Xml:
<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>
Code:
代码:
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);
}
}
Result:
结果:
回答by philu
@sa_ddam213's answer should work, but you don't need the extra ToString()method; all you need to add to your XAML is to turn IsLiveFilteringRequestedon, at least as in the .Net Framework 4.5.1.
@sa_ddam213 的答案应该有效,但您不需要额外的 ToString() 方法;您需要添加到 XAML 的所有内容就是打开IsLiveFilteringRequested,至少在 .Net Framework 4.5.1 中是这样。
<CollectionViewSource IsLiveFilteringRequested="True" x:Key="Items" Source="{Binding ElementName=UI, Path=Items}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description" />
<scm:SortDescription PropertyName="Id" />
</CollectionViewSource.SortDescriptions>
回答by Rahbek
In my case I had a list of enums that had to be converted and then ordered. My code ended up looking like this with the help of the other answers.
就我而言,我有一个必须转换然后排序的枚举列表。在其他答案的帮助下,我的代码最终看起来像这样。
<CollectionViewSource x:Key="MyEnumList" Source="{Binding ListFromViewModel, Converter={StaticResource MyEnumConverter}}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="."/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>