开发者

Bind WPF DataGrid Directly to EntityCollection<T>

开发者 https://www.devze.com 2023-02-23 02:14 出处:网络
I understand the way wpf supports automatic updates of DataGrids and Listboxes (ie adding new items and removing removed ones) is with the INotifyCollectionChanged interface that ObservableCollection

I understand the way wpf supports automatic updates of DataGrids and Listboxes (ie adding new items and removing removed ones) is with the INotifyCollectionChanged interface that ObservableCollection implements. For some reason however I seem to be having complete success binding to an EntityCollection<T> from EntityFramework, which does not seem to implement INotifyCollectionChanged. Is there some other way WPF is doing this?

I'd just like to know how this is working.

EDIT

Here's the relevant portion from the answer. The short form is that it "just works" with WPF and WinForms bindings.

EntityCollection currently does not implement INotifyCollectionChanged, which is the “new” standard interface for notifying collection changes. However, the binding list that you can obtain for EntityCollection (and which databinding will typically get when you pass and EntityCollection as a data source) is an IBindingList which has its own “old” standard way of notifying of collection changes (namely, the ListChanged event). In ge开发者_开发百科neral, WinForms and WPF databinding knows how to deal with this interface.


Apparently there was a bug filed on this, but it was closed as "by design" (atleast not for .NET 4).

As this post explains, EntityCollection effectively uses the "old" or WinForms way of list change notification, which uses IBindingList. While WPF uses the "new" INotifyCollectionChanged interface.

This blog post explains a good way to wrap an EntityCollection with a custom ListCollectionView, which you can assign to the ItemsSource property.


You might have a look at the EntityObservableCollection which can be found in the BookLibrary sample application of the WPF Application Framework (WAF). This one ensures that the WPF Binding recognizes all updates.

0

精彩评论

暂无评论...
验证码 换一张
取 消