WPF ItemsControl数据文本排序 [英] WPF ItemsControl datacontext sorting
问题描述
好的,我有一个 ItemsControl
绑定到一个列表< IComparableObject>
,每秒列表
对象改变,所以我必须诉诸他们,所以每一秒我调用$ code> List.Sort()方法。在VS2008中检查Watch面板,我可以告诉列表
被排序,但是 ItemsControl
没有。如何使这项工作?
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?
谢谢!
推荐答案
你必须对CollectionView进行排序:
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));
您必须从列表中获取默认视图。
在这种情况下,您不必对列表进行排序,因为视图将始终进行排序。您可以添加任何您想要的SortDescriptions。
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
这篇关于WPF ItemsControl数据文本排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!