的ObservableCollection和的BindingList之间的区别 [英] difference between ObservableCollection and BindingList

查看:503
本文介绍了的ObservableCollection和的BindingList之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的的ObservableCollection 的BindingList 的区别,因为我用两个通知任何附加/删除变化的来源,但其实我不知道什么时候才能preFER一个比其他。

为什么我会选择以下比其他的呢?

 的ObservableCollection<员工> lstEmp =新的ObservableCollection<员工>();
 

 的BindingList<员工> lstEmp =新的BindingList<员工>();
 

解决方案

这是的ObservableCollection 可以从用户界面更新,完全像任何集合。真正的区别是相当简单:

的ObservableCollection< T> 工具 INotifyCollectionChanged ,当集合更改(你猜^^)规定的通知 它允许绑定引擎更新UI时,的ObservableCollection 更新。

不过,的BindingList< T> 工具 IBindingList的

IBindingList的提供集合更改通知,但不仅如此。它提供了一大堆的功能,它可以使用的UI提供了更多的东西不是根据变化唯一的用户界面的更新,如:

  • 排序
  • 搜索
  • 添加通过工厂(的AddNew成员函数)。
  • 在只读列表(CanEdit属性)

所有这些功能都无法在的ObservableCollection< T>

另一个区别是,的BindingList 继电器产品更改通知时,其项目实施 INotifyPropertyChanged的。如果一个项目提出了一个的PropertyChanged 情况下,的BindingList 将收到一个提出了一个 ListChangedEvent ListChangedType.ItemChanged oldIndex进行= NewIndex (如果项目被替换, oldIndex进行= -1 )。 的ObservableCollection 不中继项目的通知。

请注意,在Silverlight中,的BindingList 不能作为一个选项:但是,您可以使用的ObservableCollection 的ICollectionView (和 IPagedCollectionView 如果我记得很清楚)。

I want to know the difference between ObservableCollection and BindingList because I've used both to notify for any add/delete change in Source, but I actually do not know when to prefer one over the other.

Why would I choose one of the following over the other?

ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();

or

BindingList<Employee> lstEmp = new BindingList<Employee>();

解决方案

An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward:

ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated.

However, BindingList<T> implements IBindingList.

IBindingList provides notification on collection changes, but not only that. It provides a whole bunch of functionality which can be used by the UI to provide a lot more things than only UI updates according to changes, like:

  • Sorting
  • Searching
  • Add through factory (AddNew member function).
  • Readonly list (CanEdit property)

All these functionalities are not available in ObservableCollection<T>

Another difference is that BindingList relays item change notifications when its items implement INotifyPropertyChanged. If an item raises a PropertyChanged event, the BindingList will receive it an raises a ListChangedEvent with ListChangedType.ItemChanged and OldIndex=NewIndex (if an item was replaced, OldIndex=-1). ObservableCollection doesn't relay item notifications.

Note that in Silverlight, BindingList is not available as an option: You can however use ObservableCollections and ICollectionView (and IPagedCollectionView if I remember well).

这篇关于的ObservableCollection和的BindingList之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆