2013-02-20 19 views
13

Şu an CollectionViewSource ürünümüzü açıklayan bir ürün grubu halinde sıralıyor. Açıklama aynıysa, kimliğe göre sıralamak istiyorum. Önce açıklamaya göre sıralama yapıp sonra kimliğe göre nasıl belirleyebilirim?Bir CollectionViewSource öğesini bir özelliğe göre nasıl sıralarsınız, sonra da başka bir yöntemle nasıl sıralarsınız?

PropertyName = "Id" ile ikinci bir SortDescription ekleme denedim, ancak bu işe yaramadı.

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

DÜZENLEME: ID özelliği ViewModel özel oldu. Hata atılmadı.

+1

Belki de bu yardımcı olacak? http://stackoverflow.com/q/6469303/56778 –

cevap

33

'un Id için neden eklendiğinden emin olmadığından emin değilim.

Xaml: Bunun gibi

: istediğiniz kadar

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

Ben birlikte bu çalışma tam bir örnek koymak

<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> 

Kodu:

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

Sonuç:

enter image description here

+0

Burada, InitializeComponents çağrılmadan önce myVar koleksiyonunun başlatılması gerektiğine dikkat edin. – Jono

0

sa_ddam213 cevabı çalışmalıdır @, ancak ekstra ToString() yöntemi gerekmez; XAML'ınıza eklemeniz gereken tek şey en azından .Net Framework 4.5.1'de olduğu gibi IsLiveFilteringRequested'u açmaktır.

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

İlgili konular