tags:

views:

202

answers:

1

Ok, I have an ItemsControl binded to a List, each second the List objects change, so I have to resort them, so each second I call the List.Sort() method. Checking in the Watch panel in VS2008, I can tell the list gets sorted, but the ItemsControl doesn't. How can I make this work?

Thanks!

+3  A: 

You have to sort the CollectionView:

 List<MyObject> myInternalList = new List<MyObject>();
 ...
 ICollectionView colView = CollectionViewSource.GetDefaultView(myInternalList);
 colView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

You have to get the default view from the List. In this case, you don't have to sort the List, because the view will always be sorted. You can add as many SortDescriptions you want.

HTH

Roel
Thanks, works like a charm. I have another question here http://stackoverflow.com/questions/1250953/sorting-in-window-datacontext-in-wpf Maybe you can help out with this one too. Thanks!!
Carlo