0

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 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That really helped. I used a 'Sort Description' in the xaml in a resources tag, and it works perfectly. Very clean solution.
@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? :)
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.