I have a xaml listview with an item source of an observable collection that has a payload of a 'Person' object. Is there a way to get the listview to DEFAULT sort by a column if an item is added or removed from the collection? So, say the Person object has a property of 'Age'. When an item is added or removed, how to I get the listview to automatically sort by age? I've been looking for a solution and I do not see one.
1 Answer
Just on addItem, and removeItem functions, add the following at the end :
SortDescription mySort = new SortDescription(Age, ListSortDirection.Ascending);
this.ListView1.Items.SortDescriptions.Clear();
this.ListView1.Items.SortDescriptions.Add(mySort);
This will add a sorting on "Age" Property.
3 Comments
nikotromus
That really helped. I used a 'Sort Description' in the xaml in a resources tag, and it works perfectly. Very clean solution.
Nicolas
@nikotromus I tried it also via XAML only solution, but it doesn't work for me - may I ask you to provide your solution as an example via separate answer? :)
Nicolas
It isn't needed to re-apply the
SortDescription at each add item, as it works for newly added items automatically. At item remove, nothing has to be resorted, obviously. But if only a property of an item changes, the SortDescription has to be re-applied, to apply the sorting on the updated values!