How do you sort a CollectionViewSource by one property, then by another as a tiebreak?

后端 未结 3 1798
北海茫月
北海茫月 2021-01-07 22:35

Currently, my CollectionViewSource sorts a collection of items by description. If the description is the same, I want to sort based on ID. How can I specify to sort by descr

相关标签:
3条回答
  • 2021-01-07 22:53

    @sa_ddam213's answer should work, but you don't need the extra ToString()method; all you need to add to your XAML is to turn IsLiveFilteringRequested on, at least as in the .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>
    

    0 讨论(0)
  • 2021-01-07 23:03

    I'm not sure why adding the SortDescription for Id does not work as it should work fine.

    Like this:

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

    I put together a full example of this working as you want:

    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>
    

    Code:

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

    Result:

    enter image description here

    0 讨论(0)
  • 2021-01-07 23:16

    In my case I had a list of enums that had to be converted and then ordered. My code ended up looking like this with the help of the other answers.

    <CollectionViewSource x:Key="MyEnumList" Source="{Binding ListFromViewModel, Converter={StaticResource MyEnumConverter}}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="."/>
        </CollectionViewSource.SortDescriptions>                
    </CollectionViewSource>
    
    0 讨论(0)
提交回复
热议问题