WPF ItemsControl datacontext sorting

冷暖自知 提交于 2019-12-12 05:27:33

问题


Ok, I have an ItemsControl binded to a List<IComparableObject>, 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!


回答1:


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



来源:https://stackoverflow.com/questions/1243274/wpf-itemscontrol-datacontext-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!