2013-02-20 9 views
13

現在、私のCollectionViewSourceは説明の項目のコレクションをソートします。説明が同じであれば、IDに基づいてソートする必要があります。最初に説明で並べ替え、次にIDで並べ替えるように指定するにはどうすればよいですか?CollectionViewSourceを1つのプロパティでソートし、次に別のプロパティでソートする方法はありますか?

PropertyName = "Id"の2番目のSortDescriptionを追加しようとしましたが、機能しませんでした。

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

EDIT: IDプロパティはviewmodelの上のプライベートました。スローされたエラーはありません。

+1

おそらくこれは役立つのだろうか? http://stackoverflow.com/q/6469303/56778 –

答えて

33

Idに追加するのがなぜうまくいくのかわかりません。

XAML:このよう

:あなたが望むよう

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

は、私は一緒にこの作業の完全な例を置く

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

結果:sa_ddam213の答え@

enter image description here

+0

ここでは、InitializeComponentsが呼び出される前にmyVarコレクションをインスタンス化する必要があることに注意してください。 – Jono

0

が動作するはずですが、あなたは余分の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> 

関連する問題