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
I'm not sure why adding the SortDescription for Id does not work as it should work fine.
Like this:
I put together a full example of this working as you want:
Xaml:
Code:
public partial class MainWindow : Window
{
private ObservableCollection myVar = new ObservableCollection();
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 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:
