ObservableCollection 和 BindingList 之间的区别
我想知道 ObservableCollection
和 BindingList
之间的区别,因为我使用两者来通知源中的任何添加/删除更改,但我实际上不知道何时更喜欢一个比另一个。
为什么我会选择以下一项而不是另一项?
ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();
或者
BindingList<Employee> lstEmp = new BindingList<Employee>();
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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ObservableCollection
可以像任何集合一样从 UI 进行更新。真正的区别相当简单:ObservableCollection
实现了INotifyCollectionChanged
,它在集合更改时提供通知(您猜对了 ^^)它允许绑定引擎在
ObservableCollection
更新时更新 UI。但是,
BindingList
实现IBindingList
。IBindingList
提供有关集合更改的通知,但不仅如此。它提供了一大堆功能,UI 可以使用这些功能来提供比仅根据更改进行 UI 更新更多的功能,例如:所有这些功能在
ObservableCollection
中均不可用。另一个区别是
BindingList
在其项实现INotifyPropertyChanged
时中继项更改通知代码>.如果某个项目引发PropertyChanged
事件,则BindingList
将接收该事件,并引发ListChangedEvent
以及ListChangedType.ItemChanged
和OldIndex=NewIndex
(如果替换了某个项目,OldIndex=-1
)。ObservableCollection
不会转发项目通知。请注意,在 Silverlight 中,
BindingList
不可用作选项:但是,您可以使用ObservableCollection
和ICollectionView
(以及IPgedCollectionView
代码>如果我没记错的话)。An
ObservableCollection
can be updated from UI exactly like any collection. The true difference is rather straightforward:ObservableCollection<T>
implementsINotifyCollectionChanged
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>
implementsIBindingList
.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:All these functionalities are not available in
ObservableCollection<T>
Another difference is that
BindingList
relays item change notifications when its items implementINotifyPropertyChanged
. If an item raises aPropertyChanged
event, theBindingList
will receive it an raises aListChangedEvent
withListChangedType.ItemChanged
andOldIndex=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 useObservableCollection
s andICollectionView
(andIPagedCollectionView
if I remember well).实际区别在于 BindingList 适用于 WinForms,而 ObservableCollection 适用于 WPF。
从 WPF 的角度来看,BindingList 没有得到适当的支持,除非确实需要,否则您永远不会在 WPF 项目中真正使用它。
The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF.
From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.
最重要的差异(例如有关所包含元素的功能和更改通知)已由接受的答案提到,但还有更多差异,也值得一提:
性能
当调用
AddNew
时,BindingList
通过IndexOf
查找来搜索添加的项目。如果T
实现了INotifyPropertyChanged
,则也会通过IndexOf
搜索已更改元素的索引(尽管只要相同的项目就不会进行新的查找)反复改变)。如果您在集合中存储数千个元素,则ObservableCollection
(或具有 O(1) 查找成本的自定义IBindingList
实现)可能更可取。完整性
IBindingList
接口是一个巨大的接口(可能不是最简洁的设计),并且允许实现者仅实现其功能的子集。例如,AllowNew
、SupportsSorting
和SupportsSearching
属性告诉是否AddNew
、ApplySort
可以分别使用Find
方法。BindingList
本身不支持排序,这常常让人们感到惊讶。实际上它提供了一些虚拟方法让派生类添加缺少的功能。DataView
类是完整的IBindingList
实现的示例;但是,它首先不适用于类型化集合。 WinForms 中的BindingSource
类是一个混合示例:如果它包装另一个支持排序的IBindingList
实现,它就支持排序。ObservableCollection
已经是INotifyCollectionChanged
接口(只有一个事件)的完整实现。它也有虚拟成员,但ObservableCollection
通常是出于与其基Collection
类相同的原因而派生的:用于自定义添加/删除项目(例如,在数据模型集合),而不是调整绑定功能。复制与包装
ObservableCollection
和BindingList
都有一个构造函数,它接受已存在的列表。尽管它们在由另一个集合实例化时的行为有所不同:BindingList
充当所提供列表的可观察包装器,以及在BindingList
BindingList
上执行的更改;T>
也将反映在底层集合上。ObservableCollection
将新的List
实例传递给基本Collection
构造函数,并复制将原始集合添加到这个新列表中。当然,如果T
是引用类型,则元素上的更改将从原始集合中可见,但集合本身不会更新。The most important differences such as features and change notifications about the contained elements are already mentioned by the accepted answer but there are more, which also worth mentioning:
Performance
When
AddNew
is called,BindingList<T>
searches for the added item by anIndexOf
lookup. And ifT
implementsINotifyPropertyChanged
the index of a changed element is also searched byIndexOf
(though there is no new lookup as long as the same item changes repeatedly). If you store thousands of elements in the collection, thenObservableCollection<T>
(or a customIBindingList
implementation with O(1) lookup cost) can be more preferable.Completeness
The
IBindingList
interface is a huge one (maybe not the cleanest design) and allows the implementors to implement only a subset of its features. For example, theAllowNew
,SupportsSorting
andSupportsSearching
properties tell whetherAddNew
,ApplySort
andFind
methods can be used, respectively. It often surprises people thatBindingList<T>
itself does not support sorting. Actually it provides some virtual methods letting the derived classes add the missing features. TheDataView
class is an example for a fullIBindingList
implementation; however, it is not for typed collections in the first place. And theBindingSource
class in WinForms is a hybrid example: it supports sorting if it wraps anotherIBindingList
implementation, which supports sorting.ObservableCollection<T>
is already a complete implementation of theINotifyCollectionChanged
interface (which has only a single event). It also has virtual members butObservableCollection<T>
is typically derived for the same reason as its baseCollection<T>
class: for customizing add/remove items (eg. in a data model collection) rather than adjusting binding features.Copy vs. wrapping
Both
ObservableCollection<T>
andBindingList<T>
have a constructor, which accepts an already existing list. Though they behave differently when they are instantiated by another collection:BindingList<T>
acts as an observable wrapper for the provided list, and the changes performed on theBindingList<T>
will be reflected on the underlying collection as well.ObservableCollection<T>
on the other hand passes a newList<T>
instance to the baseCollection<T>
constructor and copies the elements of the original collection into this new list. Of course, ifT
is a reference type changes on the elements will be visible from the original collection but the collection itself will not be updated.ObservableCollection
和BindingList
之间还有一个巨大差异,很方便,并且可以成为该主题的出价决策因素:BindingList
列表更改处理程序:ObservableCollection
集合更改:以上结论是关于模型类中实现的
INotifyPropertyChanged
。默认情况下,如果项目中的属性发生更改,则不会引发更改事件。One More big difference between
ObservableCollection
andBindingList
that comes handy, and can be a bid decision factor on the topic :BindingList
List Change Handler:ObservableCollection
Collection change:Above conclusion are in regards of
INotifyPropertyChanged
implemented in model classes. By default none raises the changed event if a property is changed in an item.两者都有优点和缺点,需要一些时间才能发现。
我在使用
BindingList
时遇到了一些麻烦,因为更改通知事件仅在删除项目后发生,并且仅提供索引(这意味着您必须跟踪哪个对象)如果您实施了某种删除后机制,则位于哪个位置)。另一方面,ObservableCollection
为您提供已删除项目的列表。BindingList
有一个方便的AddNew()
方法,允许派生类实现工厂模式,例如根据父集合的值初始化新项目(例如,如果集合包含子项,则为父项的外键)。另请注意,使用 (
ToBindingList
实体框架中的扩展),以及返回的(派生的)BindingList 实现了普通功能所缺乏的功能,例如排序。Both have advantages and drawbacks that take some time to discover.
I had some trouble with
BindingList
because the change notification event occurs only after an item has been removed and provides only an index (which means that you have to keep track of which object was at which location if you have implemented some post-removal mechanism).ObservableCollection
, on the other hand, gives you a list of the removed items.BindingList
has a handyAddNew()
method that allows a derived class to implement a factory pattern, e.g. to initialize the new items with values depending on the parent collection (for instance, a foreign key to the parent if the collection contains child items).Note also that it is very easy to get a
BindingList
from anObservableCollection
, using (ToBindingList
extension in Entity Framework), and that the returned (derived)BindingList
implements features, like sorting, that lack in the plain vanilla one.