Sorting in WPF ListView

Sorting in WPF ListView

Old forum URL: forums.lhotka.net/forums/t/5953.aspx


jtradke posted on Monday, December 08, 2008

I'm trying to use a ListView to do dynamic sorting whenever a column header is clicked on via ListView.Items.SortDescriptions, but this only works if my ListView is *not* bound directly to my BusinessListBase<> - so I wrap my BLB in a Collection<>.

The problem there, is that my interface is not bound directly to the collection object, so I need to keep adds and removes in sync between the Collection and the BLB.

Is there any way I can bind directly to the BLB and have sorting with the SortDescriptions?  Is there a different way?

JZuerlein replied on Tuesday, December 09, 2008

I would look at the SortedBindingList<T> class.  You basically create a collection and pass it into the constructor of SortedBindingList.  Bind the ListView to the SortedBindingList.  When adds and removes are performed on the SortedBindingList, they will affect the underlying collection as well.  As I understand it, the removes should be performed with SortedBindingList.RemoveAt() so that the ListChanged event is raised.

Jeff

daniel_higgins replied on Tuesday, December 09, 2008

When I used CollectionViewSource.GetDefaultView, I got back a CollectionView that was not sortable. However, when I used

ListCollectionView myView = new ListCollectionView(parent.ChildCollection);

I was able to myView.SortDescriptions.Add(...) to apply a sort.

Copyright (c) Marimer LLC