什么时候值得使用BindingSource? [英] When is it worth using a BindingSource?

查看:137
本文介绍了什么时候值得使用BindingSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为BindingSource类完全可以理解,即在数据源和UI控件之间提供一个间接层。它实现了IBindingList接口,因此也提供了对排序的支持。我经常使用它,没有太多的问题。但我想知道如果我比我应用更频繁地使用它。也许一个例子会有所帮助。



假设我在表单上只有一个简单的文本框(使用WinForms),我想将该文本框绑定到一个简单的属性在返回一个字符串的类中。在这种情况下,是否值得使用BindingSource?



现在我的表单上有一个网格,我想绑定到一个DataTable。我应该使用BindingSource吗?



在后一种情况下,我可能会使用BindingSource作为数据表,从我可以收集,提供与BindingSource本身相同的功能。当添加行,删除等时,DataTable将触发正确的事件,以便网格将自动更新。



但是在第一种情况下,文本框被绑定到一个字符串,我可能会有包含字符串属性的类实现INotifyPropertyChanged,以便它可以在字符串更改时触发PropertyChanged事件。我会使用BindingSource,以便它可以收听这些PropertyChanged事件,以便它可以在字符串更改时自动更新文本框。



这个声音到目前为止如何?我仍然觉得我的理解有一个差距,阻止我看到整个画面。到目前为止,这是一个很模糊的问题,所以我会尝试一些更具体的问题 - 理想的答案将会引用上面的例子或类似的例子。





(2)似乎开发人员只是假设DataTable类将执行正确的事情,在正确的时间触发PropertyChanged事件。如何知道数据源是否能够做到这一点?有没有一个特定的接口,数据源应该实现,以便开发人员能够承担这种行为?



(3)控制是绑定到什么当考虑是否使用BindingSource?还是仅仅是影响决策的数据源?也许答案是(这似乎足够合乎逻辑):控制需要足够聪明才能收听PropertyChanged事件,否则需要BindingSource。那么如何判断控制是否有能力呢?再次,开发人员可以找到一个特定的界面,控件必须实现吗?



过去曾经引起我的这种混淆,始终使用BindingSource。但是我想更好地了解何时使用它,所以只有在必要的时候才这样做。

解决方案

也有一些关于这个问题的混乱。

当我使用datatables这些实现所有的接口。

但是我总是使用bindingsource只是为了确保.. :)



有一些参数为什么我可以想到


  1. 同一个视图记录集。 (即2个具有差异排序顺序/过滤器的网格)

  2. 过滤,排序,而不更改记录本身的排序顺序(过滤/排序)

  3. 出于性能原因,能够禁用一段时间的绑定。 (当表中有大的更新时,不要听所有的IXXChanged事件)

  4. IErrorprovider从未在没有绑定源的情况下为我工作,但这可能是我的错。 >


I think I understand well enough what the BindingSource class does - i.e. provide a layer of indirection between a data source and a UI control. It implements the IBindingList interface and therefore also provides support for sorting. And I've used it frequently enough, without too many problems. But I'm wondering if I use it more often than I should. Perhaps an example would help.

Let's say I have just a simple textbox on a form (using WinForms), and I'd like to bind that textbox to a simple property inside a class that returns a string. Is it worth using a BindingSource in this situation?

Now let's say I have a grid on my form, and I'd like to bind it to a DataTable. Should I use a BindingSource now?

In the latter case, I probably would not use a BindingSource, as a DataTable, from what I can gather, provides the same functionality that the BindingSource itself would. The DataTable will fire the the right events when a row is added, deleted, etc so that the grid will automatically update.

But in the first case with the textbox being bound to a string, I would probably have the class that contains the string property implement INotifyPropertyChanged, so that it could fire the PropertyChanged event when the string changes. I would use a BindingSource so that it could listen to these PropertyChanged events so that it could update the textbox automatically when the string changes.

How does this sound so far? I still feel like there's a gap in my understanding that's preventing me from seeing the whole picture. This has been a pretty vague question so far, so I'll try to ask some more specific questions - ideally the answers will reference the above examples or something similar...

(1) Is it worth using a BindingSource in either of the above examples?

(2) It seems that developers just "assume" that the DataTable class will do the right thing, in firing PropertyChanged events at the right time. How does one know if a data source is capable of doing this? Is there a particular interface that a data source should implement in order for developers to be able to assume this behaviour?

(3) Does it matter what Control is being bound to, when considering whether or not to use a BindingSource? Or is it only the data source that should affect the decision? Perhaps the answer is (and this would seem logical enough): the Control needs to be intelligent enough to listen to the PropertyChanged events, otherwise a BindingSource is required. So how does one tell if the Control is capable of doing this? Again, is there a particular interface that developers can look for that the Control must implement?

It is this confusion that has, in the past, led to me always using a BindingSource. But I'd like to understand better exactly when to use one, so that I do so only when necessary.

解决方案

Hi I also have some confusion about the subject.
When I use datatables those implement all the interfaces.
However I always use the bindingsource just to be sure.. :)

There are some arguments why that I can think of

  1. Multiple views on the same recordset. (ie. 2 grids with diffent sort orders/filters)
  2. Filtering,Sorting while not changing the sort order of the records themselves (Filter/Sort)
  3. Ability to disable binding for a while for performance reasons. (when there are big updates in the table, don'y listen to all the IXXChanged events)
  4. IErrorprovider never worked for me without a bindingsource, however this could be my fault.

这篇关于什么时候值得使用BindingSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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