31

BindingSource クラスが何をするのか、つまり、データ ソースと UI コントロールの間に間接レイヤーを提供するということを十分に理解していると思います。IBindingList インターフェイスを実装しているため、並べ替えもサポートされます。そして、あまり問題なく、十分に頻繁に使用しました。しかし、必要以上に頻繁に使用するかどうかは疑問です。おそらく、例が役立つでしょう。

フォームに単純なテキスト ボックス (WinForms を使用) があり、そのテキスト ボックスを、文字列を返すクラス内の単純なプロパティにバインドしたいとします。この状況で BindingSource を使用する価値はありますか?

ここで、フォームにグリッドがあり、それを DataTable にバインドしたいとします。今すぐ BindingSource を使用する必要がありますか?

後者の場合、収集できるものから DataTable として BindingSource を使用しない可能性が高く、BindingSource 自体が提供するのと同じ機能を提供します。グリッドが自動的に更新されるように、行が追加、削除などされると、DataTable は適切なイベントを発生させます。

しかし、テキスト ボックスが文字列にバインドされている最初のケースでは、おそらく文字列プロパティを含むクラスに INotifyPropertyChanged を実装させ、文字列が変更されたときに PropertyChanged イベントを発生させることができます。これらの PropertyChanged イベントをリッスンできるように BindingSource を使用して、文字列が変更されたときにテキスト ボックスを自動的に更新できるようにします。

これまでのところ、これはどのように聞こえますか?全体像を見るのを妨げている私の理解にはまだギャップがあるように感じます. これまでのところ、これはかなり漠然とした質問だったので、より具体的な質問をいくつかしてみます - 理想的には、回答は上記の例または類似のものを参照します...

(1) 上記の例のいずれかで BindingSource を使用する価値はありますか?

(2) 開発者は、適切なタイミングで PropertyChanged イベントを発生させるという点で、DataTable クラスが適切なことを行うと「想定」しているようです。データソースがこれを行うことができるかどうかをどのように知ることができますか? 開発者がこの動作を想定できるようにするために、データ ソースが実装する必要がある特定のインターフェイスはありますか?

(3) BindingSource を使用するかどうかを検討するとき、どの Control にバインドされているかは重要ですか? それとも、決定に影響を与えるのはデータ ソースだけですか? おそらく答えは (そしてこれは十分に論理的に思えるでしょう): Control は PropertyChanged イベントをリッスンするのに十分なほどインテリジェントである必要があります。それ以外の場合は BindingSource が必要です。では、コントロールがこれを行うことができるかどうかをどのように判断しますか? 繰り返しますが、開発者が探すことができ、コントロールが実装する必要がある特定のインターフェイスはありますか?

過去に、常にBindingSource を使用するようになったのは、この混乱です。しかし、必要な場合にのみ使用するように、いつ使用するかをより正確に理解したいと思います。

4

3 に答える 3

6

Pretty old question. Wonder why anyone hasn't answered it till now. OK, I'll try and share things from my experience.

A BindingSource is more than just a way to bind controls to collections. After having worked in WinForms for over a decade, the best features of a BindingSource that I like the most include:

  1. Binding (of course!)
  2. Currency management (I'll come to that in a second)
  3. A BindingSource can act as a data source of another BindingSource.

To fully appreciate these features, I'll explain them in the context of a DataSet, which is by far the most common type of data source used in WinForms, especially in line-of-business apps.

Currency management boils down the concept of current record. A DataTable is just a collection of DataRows, i.e. there is no concept of current record in DataTables. Same is the case of DataView (on a side note, you cannot directly bind to a DataTable; when you do that, it actually binds to the DefaultView property of that DataTable, which is a DataView. You could create your own DataView too).

Currency management really proves handy in case of Master/Detail kind of UI. So let's say you have a ListBox of Students in the left pane (Master), and several TextBoxes, ComboBoxes, CheckBoxes etc. in the right pane, with a grid of selected student's courses (Detail). In your DataSet, you have two DataTables named Student and Courses. For the sake of simplicity, I'm avoiding a gerund (Student_Course) here. The Course table has a foreign key StudentID. Here's how you setup binding here (note how all the 3 features I listed above are used in the setup below):

  1. Add two BindingSource controls to your form, named bsStudent and bsCourses.
  2. Set DataSource of bsStudent to Student DataTable.
  3. Set DataSource of bsCourses to bsStudent!
  4. In the DataMember property, you'll see the name of the relation that exists in the DataSet between our two tables. Select it!
  5. Set the binding of individual atomic controls to bsStudent's properties.
  6. Set the DataSource of courses grid bsCourses.

And you're done. Without writing a single line of code (so to speak), you have successfully created a master-details view. The BindingSource control will now take care of the current record in Students list and update not only the atomic controls (TextBoxes, ComboBoxes etc.), but also the courses grid, which will automatically update its contents to show the courses of currently selected student.

This, my friend, is the role of BindingSource (among other nice things like sorting, filtering etc.) which I like the most. Without involving a BindingSource in-between your controls and the data store, you'd not have the concept of current record and therefore would manually have to manage keeping all the UI in sync.

于 2015-03-30T06:05:15.853 に答える
4

こんにちは、私もこの件について混乱しています。
データテーブルを使用すると、それらはすべてのインターフェースを実装します。
ただし、念のために常にバインディングソースを使用します.. :)

考えられる理由がいくつかあります

  1. 同じレコードセットに対する複数のビュー。(つまり、異なるソート順/フィルターを持つ 2 つのグリッド)
  2. レコード自体のソート順を変えずにフィルタリング、ソートする (Filter/Sort)
  3. パフォーマンス上の理由から、バインディングをしばらく無効にする機能。(テーブルに大きな更新がある場合、すべての IXXChanged イベントをリッスンしないでください)
  4. IErrorprovider は、バインディングソースなしでは機能しませんでしたが、これは私のせいかもしれません。
于 2010-06-15T05:47:25.190 に答える
3

また、BindingSource を使用して、INotifyPropertyChanged イベントを実装できるビジネス オブジェクトにバインドできるので、データが変更された場合 (自分のコードまたは他の誰かのコードによって)、UI が自動的に更新を反映できることを追加したいと思います。

于 2011-09-23T19:52:56.280 に答える