nikotromus
nikotromus

Reputation: 1074

Sorting a ListView by a default column

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.

Upvotes: 0

Views: 738

Answers (1)

Siegfried.V
Siegfried.V

Reputation: 1595

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.

Upvotes: 1

Related Questions