WPF BindingListCollectionView 到 ListCollectionView(数据表作为 ItemsSource)
我想在具有 DataTable 作为 ItemsSource 的 ListView 上进行自定义排序:
myListView.ItemsSource = (data as DataTable);
这是我的排序函数的第一行:
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
ListCollectionView coll = (ListCollectionView)CollectionViewSource.GetDefaultView(view);
第二行抛出一个类似以下的错误:
无法将“System.Windows.Data.BindingListCollectionView”转换为“System” .Windows.Data.ListCollectionView”
有人有解决方案吗?谢谢 4 个答案
I want to do custom sorting on a ListView which has a DataTable as ItemsSource:
myListView.ItemsSource = (data as DataTable);
And this are the first lines of my sorting function:
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
ListCollectionView coll = (ListCollectionView)CollectionViewSource.GetDefaultView(view);
The second line throws an execption like:
Unable to cast "System.Windows.Data.BindingListCollectionView" to "System.Windows.Data.ListCollectionView"
Has anyone a solution? Thx 4 answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它返回一个 ICollectionView,而不是 ListCollectionView。您很可能需要视图之上的视图来获取 ListCollectionView 所具有的功能。由于 ICollectionView 实现了 CollectionChanged,因此您不会想使用 BindingListCollectionView。
尽管另一种选择是:
如果您只想要一个视图。
如果直接绑定到 WPF 控件,最好直接绑定到它而不创建 BindingListCollectionView/ListCollectionView,因为 DefaultView 已经允许对 DataTable 进行排序。
希望有帮助,
TamusJRoyce
It returns an ICollectionView that is not a ListCollectionView. You most likely want a view on top of a view to get the features that ListCollectionView has. And since ICollectionView implements CollectionChanged, you wouldn't want to use BindingListCollectionView.
Although an alternative would be:
If you only wanted only one view.
If you are binding directly to a WPF control, it is best to bind directly to it without making a BindingListCollectionView/ListCollectionView, as DefaultView already allows sorting of the DataTable.
Hopefully Helpful,
TamusJRoyce