2013-02-20 197 views
13

目前,我的CollectionViewSource按描述對項目集合進行排序。如果描述相同,我想根據ID進行排序。我如何指定先按描述進行排序,然後按ID排序?如何對一個屬性對CollectionViewSource進行排序,然後將另一個屬性作爲tiebreak進行排序?

我試着用PropertyName =「Id」添加第二個SortDescription,但那沒有奏效。

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

編輯: ID屬性是在視圖模型私人。沒有錯誤拋出。

+1

或許這將幫助? http://stackoverflow.com/q/6469303/56778 – 2013-02-21 00:08:30

回答

33

我不確定爲什麼要爲Id添加SortDescription不起作用,因爲它應該可以正常工作。

像這樣:

<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); 
    } 
} 

結果:

enter image description here

+0

值得注意的是,在調用InitializeComponents之前,需要實例化myVar集合。 – Jono 2017-02-26 18:17:02

0

@ sa_ddam213的答案應該工作,但你並不需要額外的ToString()方法;所有您需要添加到您的XAML的是至少在.Net Framework 4.5.1中打開IsLiveFilteringRequested

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

相關問題