目前,我的CollectionViewSource按说明对项目集合进行排序。如果描述相同,我想根据ID进行排序。如何指定先按描述排序,然后按ID排序?
我尝试添加第二个SortDescription,其中PropertyName =" Id",但这还没有奏效。
<CollectionViewSource x:Key="Items" Source="{Binding Items}" >
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
编辑:ID属性在viewmodel上是私有的。没有错误抛出。
答案 0 :(得分:35)
我不确定为SortDescription
添加Id
为什么不起作用,因为它应该可以正常工作。
像这样:
<CollectionViewSource x:Key="Items" Source="{Binding ElementName=UI, Path=Items}" >
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description" />
<scm:SortDescription PropertyName="Id" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
我根据你的需要整理了一个完整的例子:
的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);
}
}
结果:
答案 1 :(得分:1)
在我的情况下,我有一个必须转换然后排序的枚举列表。在其他答案的帮助下,我的代码最终看起来像这样。
<CollectionViewSource x:Key="MyEnumList" Source="{Binding ListFromViewModel, Converter={StaticResource MyEnumConverter}}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="."/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
答案 2 :(得分:0)
@ 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>