WPF BindingListCollectionView 到 ListCollectionView(数据表作为 ItemsSource)

发布于 2024-09-02 12:47:19 字数 489 浏览 9 评论 0原文

我想在具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-09-09 12:47:19

它返回一个 ICollectionView,而不是 ListCollectionView。您很可能需要视图之上的视图来获取 ListCollectionView 所具有的功能。由于 ICollectionView 实现了 CollectionChanged,因此您不会想使用 BindingListCollectionView。

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

ListCollectionView coll = new ListCollectionView(CollectionViewSource.GetDefaultView(view));

尽管另一种选择是:

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

BindingListCollectionView coll = new BindingListCollectionView(view);

如果您只想要一个视图。

如果直接绑定到 WPF 控件,最好直接绑定到它而不创建 BindingListCollectionView/ListCollectionView,因为 DefaultView 已经允许对 DataTable 进行排序。

Binding binding = new Binding() { Source = (myListView.ItemsSource as DataTable) };

this.myListView.SetBinding(myListView.ItemsSourceProperty, binding);

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

view.Sort = "Age";

希望有帮助,

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.

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

ListCollectionView coll = new ListCollectionView(CollectionViewSource.GetDefaultView(view));

Although an alternative would be:

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

BindingListCollectionView coll = new BindingListCollectionView(view);

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.

Binding binding = new Binding() { Source = (myListView.ItemsSource as DataTable) };

this.myListView.SetBinding(myListView.ItemsSourceProperty, binding);

DataView view = (myListView.ItemsSource as DataTable).DefaultView;

view.Sort = "Age";

Hopefully Helpful,

TamusJRoyce

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文