2009-08-09 9 views
1

Eine andere Frage im Zusammenhang mit this one.Sortierung in Fenster Datenkontext in WPF

Ich habe eine List<SortableObjects> das ist die DataContext meines MainWindow. Ich verwende diese Liste, um eine ListBox und eine ComboBox zu füllen. Wenn ich die Artikel sortiere, werden sowohl die ComboBox als auch die ListView in Ordnung gebracht. Aber jetzt muss ich die ComboBox auf eine andere Weise als die ListView sortiert werden. I. E. Wenn das Objekt eine Person wäre, müsste ich sie in der ComboBox nach Nachname, aber in der ListView, nach Geburtstag sortieren. Wie kann ich das erreichen?

Danke!

Antwort

2

Verwenden CollectionViewSource s für jede der getrennten Ordnungen Sie wollen:

<UserControl.Resources> 
    <CollectionViewSource x:Key="ComboBoxSource" Source="{Binding YourUnderlyingCollection}"> 
     <CollectionViewSource.SortDescriptions> 
      <scm:SortDescription PropertyName="SomeProperty"/> 
     </CollectionViewSource.SortDescriptions> 
    </CollectionViewSource> 

    <CollectionViewSource x:Key="ListBoxSource" Source="{Binding YourUnderlyingCollection}"> 
     <CollectionViewSource.SortDescriptions> 
      <scm:SortDescription PropertyName="SomeOtherProperty"/> 
     </CollectionViewSource.SortDescriptions> 
    </CollectionViewSource> 
</UserControl.Resources> 

<ComboBox ItemsSource="{Binding Source={StaticResource ComboBoxSource}}"/> 

<ListBox ItemsSource="{Binding Source={StaticResource ListBoxSource}}"/> 
+0

perfekt gearbeitet. Vielen Dank. – Carlo