C# List<T> 与 BindingList<T> 优点/缺点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2243950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List<T> vs BindingList<T> Advantages/DisAdvantages
提问by Jon
Can someone describe what the difference between the two are for my project.
有人可以描述我的项目两者之间的区别。
Currently I have a List<MyClass>
and set the BindingSource to that and a DataGridView to the BindingSource.
目前我有一个List<MyClass>
并将 BindingSource 设置为那个,并将 DataGridView 设置为 BindingSource。
I have implemented IEditableObject
so when CancelEdit is called I revert my object back to what it was with a Memberwise.Clone()
我已经实现了,IEditableObject
所以当 CancelEdit 被调用时,我将我的对象恢复到原来的状态Memberwise.Clone()
Will changing my List to a BindingList solve any of this and what are the advantages of using a BindingList?
将我的 List 更改为 BindingList 会解决任何问题吗?使用 BindingList 有什么好处?
采纳答案by Alex J
A List<>
is simply an automatically resizing array, of items of a given type, with a couple of helper functions (eg: sort). It's just the data, and you're likely to use it to run operations on a set of objects in your model.
AList<>
只是一个自动调整大小的数组,由给定类型的项目组成,带有几个辅助函数(例如:sort)。它只是数据,您可能会使用它来对模型中的一组对象运行操作。
A BindingList<>
is a wrapper around a typed list or a collection, which implements the IBindingList
interface. This is one of the standard interfaces that support two-way databinding. It works by implementing the ListChanged
event, which is raised when you add, remove, or set items. Bound controls listen to this event in order to know when to refresh their display.
ABindingList<>
是类型列表或集合的包装器,它实现了IBindingList
接口。这是支持双向数据绑定的标准接口之一。它通过实现ListChanged
在您添加、删除或设置项目时引发的事件来工作。绑定控件侦听此事件以了解何时刷新其显示。
When you set a BindingSource's DataSource to a List<>
, it internally creates a BindingList<>
to wrap your list. You may want to pre-wrap your list with a BindingList<>
yourself if you want to access it outside of the BindingSource, but otherwise it's just the same. You can also inherit from BindingList<>
to implement special behavior when changing items.
当您将 BindingSource 的 DataSource 设置为 a 时List<>
,它会在内部创建 aBindingList<>
来包装您的列表。BindingList<>
如果您想在 BindingSource 之外访问它,您可能想用自己预先包装您的列表,否则它是一样的。您还可以继承 fromBindingList<>
以在更改项目时实现特殊行为。
IEditableObject
is handled by the BindingSource. It'll call BeginEdit on any implementing object when you change the data in any bound control. You can then call EndEdit/CancelEdit on the BindingSource and it will pass it along to your object. Moving to a different row will call EndEdit as well.
IEditableObject
由 BindingSource 处理。当您更改任何绑定控件中的数据时,它将在任何实现对象上调用 BeginEdit。然后,您可以在 BindingSource 上调用 EndEdit/CancelEdit,它会将其传递给您的对象。移动到不同的行也会调用 EndEdit。
回答by Gerrie Schenck
A BindingList allows two-way databinding by using events, a List does not fire events when its collection changes.
BindingList 允许使用事件进行双向数据绑定,List 在其集合更改时不会触发事件。
I don't think it will fix your particular problem.
我认为它不会解决您的特定问题。