BindingContext クラス

Control クラスから継承されるすべてのオブジェクトについて、 BindingManagerBase オブジェクトのコレクションを管理します。

この型のすべてのメンバの一覧については、BindingContext メンバ を参照してください。

System.Object
   System.Windows.Forms.BindingContext

Public Class BindingContext
   Implements ICollection, IEnumerable
[C#]
public class BindingContext : ICollection, IEnumerable
[C++]
public __gc class BindingContext : public ICollection, IEnumerable
[JScript]
public class BindingContext implements ICollection, IEnumerable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

Control クラスから継承される各オブジェクトは、1 つの BindingContext オブジェクトを保持できます。 BindingContext は、そのコントロールおよびコンテナ内のすべてのコントロールの BindingManagerBase オブジェクトを管理します。コンテナ内のデータ連結コントロールが使用するデータ ソースの BindingManagerBase を作成したり返したりするには、 BindingContext を使用します。通常は、 Form クラスの BindingContext を使用して、フォームのデータ連結コントロールの BindingManagerBase オブジェクトを返します。 GroupBoxPanelTabControl などのコンテナ コントロールを使用してデータ連結コントロールを格納する場合は、コンテナ コントロールおよびコンテナ内にあるコントロールだけで構成された BindingContext を作成できます。これにより、フォームの各部分をフォーム自体の BindingManagerBase オブジェクトで管理できます。同じデータ ソースに対して複数の BindingManagerBase オブジェクトを作成する場合の詳細については、 BindingContext コンストラクタのトピックを参照してください。

特定の BindingManagerBase オブジェクトを返すには、次のいずれかのパラメータを Item プロパティに渡す必要があります。

  • 目的の BindingManagerBase でナビゲーション パスを必要としない場合は、データ ソースのみ。たとえば BindingManagerBase によって、 DataSource として ArrayList または DataTable を使用する Binding オブジェクトのセットを管理する場合、ナビゲーション パスは不要です。ナビゲーション パスの作成の詳細については、次の項目を参照してください。
  • データ ソースとナビゲーション パス。データ ソースで複数のオブジェクトが格納されている Binding オブジェクトのセットを BindingManagerBase が管理する場合は、ナビゲーション パス (Item プロパティの dataMember パラメータに設定) が必要です。たとえば、 DataSet には、 DataRelation オブジェクトによってリンクされた複数の DataTable オブジェクトを格納できます。このような場合、 BindingContext で正しい BindingManagerBase を返すためにはナビゲーション パスが必要です。

使用できるデータ ソースの一覧については、 Binding クラスのトピックを参照してください。

BindingManagerBase クラスは抽象クラスであるため、リターン オブジェクトの Type は、 CurrencyManager または PropertyManager のいずれかです。データ ソースが、オブジェクトのリストではなく単一のプロパティだけを返すことのできるオブジェクトの場合は、 TypePropertyManager です。たとえば、データ ソースとして TextBox を指定すると、 PropertyManager が返されます。一方、データ ソースが IList または IBindingList を実装するオブジェクトの場合は、 CurrencyManager が返されます。

目的の BindingManagerBase によってリストを管理する場合は、ナビゲーション パスにリストの終端も指定する必要があります。たとえば、次の C# コードでは、新しい Binding オブジェクトを ControlBindingsCollection に追加することにより、 TextBox コントロールを発注表の発注日付にバインドします。ナビゲーション パスは、 TableNameRelationName 、および ColumnName で構成されます。2 行目では、 BindingContext を使用して、 BindingManagerBase が返されています。この場合は、 TableNameRelationName (リストに解決されます) を Item プロパティに渡して取得しています。

/* Create a new Binding object and add to the ControlBindingsColllection.
The navigation path (Customers.custOrders.OrderDate) used to 
create a Binding ends with a property. */
textBox1.DataBindings.Add
("Text", dataSet1, "Customers.custToOrders.OrderDate");
/* The navigation path (Customers.custToOrders) used to 
return the BindingManagerBase ends with a list. */
BindingManagerBase bmOrders = 
this.BindingContext[dataSet1, "Customers.custToOrders"];

BindingManagerBase が返されると、 Binding と同じデータ ソースを使用して、ナビゲーション パスだけを変更する必要があります。同じ BindingManagerBase を返すように見えても、結果が異なる C# コードの例を次に示します。

BindingManagerBase bmCustomers = this.BindingContext
[myDataSet, "Customers"];
// This returns a different BindingManagerBase:
BindingManagerBase bmOther = this.BindingContext
[myDataSet.Tables[Customers], ""];
// This line returns true.
Console.WriteLine(bmCustomers != bmOther);

BindingContext に渡された各オブジェクトに対しては、異なる BindingManagerBase オブジェクトが返されます。上の例では、 Item プロパティに渡されたオブジェクトが異なるため、各オブジェクトに返された BindingManagerBase が異なります。

メモ    Item プロパティを使用すると、まだ存在しない場合は、 BindingContext によって新しい BindingManagerBase が作成されます。この処理によって、リターン オブジェクトが目的のリスト (またはすべてのリスト) を管理できなくなるなどの混乱が生じる場合があります。無効な BindingManagerBase が返されないようにするには、 Contains メソッドを使用して、目的の BindingManagerBase が既に存在するかどうかを確認します。

使用例

[Visual Basic, C#, C++] 4 つの Binding オブジェクトを作成して、5 つのコントロール (1 つの DateTimePicker コントロールと 4 つの TextBox コントロール) を複数のデータ ソースにバインドする例を次に示します。さらに、 BindingContext を使用して、各データ ソースの BindingManagerBase を取得します。

 
Protected Sub BindControls()

   ' Create two Binding objects for the first two TextBox 
   '   controls. The data-bound property for both controls 
   '   is the Text property. The data source is a DataSet 
   '   (ds). The data member is the string 
   '   "TableName.ColumnName".
   text1.DataBindings.Add(New Binding _
      ("Text", ds, "customers.custName"))
   text2.DataBindings.Add(New Binding _
      ("Text", ds, "customers.custID"))
   
   ' Bind the DateTimePicker control by adding a new Binding. 
   '   The data member of the DateTimePicker is a 
   '   TableName.RelationName.ColumnName string.
   DateTimePicker1.DataBindings.Add(New Binding _
      ("Value", ds, "customers.CustToOrders.OrderDate"))

   ' Add event delegates for the Parse and Format events to a 
   '   new Binding object, and add the object to the third 
   '   TextBox control's BindingsCollection. The delegates 
   '   must be added before adding the Binding to the 
   '   collection; otherwise, no formatting occurs until 
   '   the Current object of the BindingManagerBase for 
   '   the data source changes.
   Dim b As Binding = New Binding _
      ("Text", ds, "customers.custToOrders.OrderAmount")
   AddHandler b.Parse,  New ConvertEventHandler(AddressOf CurrencyStringToDecimal)      
   AddHandler b.Format, New ConvertEventHandler(AddressOf DecimalToCurrencyString)
   text3.DataBindings.Add(b)

   ' Get the BindingManagerBase for the Customers table.
   bmCustomers = Me.BindingContext(ds, "Customers")

   ' Get the BindingManagerBase for the Orders table using the 
   '   RelationName.
   bmOrders = Me.BindingContext(ds, "customers.CustToOrders")

   ' Bind the fourth TextBox control's Text property to the
   ' third control's Text property.
   text4.DataBindings.Add("Text", text3, "Text")

End Sub


[C#] 
protected void BindControls()
{
   /* Create two Binding objects for the first two TextBox 
      controls. The data-bound property for both controls 
      is the Text property. The data source is a DataSet 
      (ds). The data member is a navigation path in the form: 
      "TableName.ColumnName". */
   text1.DataBindings.Add(new Binding
   ("Text", ds, "customers.custName"));
   text2.DataBindings.Add(new Binding
   ("Text", ds, "customers.custID"));
   
   /* Bind the DateTimePicker control by adding a new Binding. 
      The data member of the DateTimePicker is a navigation path:
      TableName.RelationName.ColumnName string. */
   DateTimePicker1.DataBindings.Add(new 
   Binding("Value", ds, "customers.CustToOrders.OrderDate"));

   /* Add event delegates for the Parse and Format events to a 
      new Binding object, and add the object to the third 
      TextBox control's BindingsCollection. The delegates 
      must be added before adding the Binding to the 
      collection; otherwise, no formatting occurs until 
      the Current object of the BindingManagerBase for 
      the data source changes. */
      Binding b = new Binding
      ("Text", ds, "customers.custToOrders.OrderAmount");
   b.Parse+=new ConvertEventHandler(CurrencyStringToDecimal);
   b.Format+=new ConvertEventHandler(DecimalToCurrencyString);
   text3.DataBindings.Add(b);

   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this.BindingContext [ds, "Customers"];

   /* Get the BindingManagerBase for the Orders table using the 
      RelationName. */ 
   bmOrders = this.BindingContext[ds, "customers.CustToOrders"];

   /* Bind the fourth TextBox control's Text property to the
   third control's Text property. */
   text4.DataBindings.Add("Text", text3, "Text");
}


[C++] 
void BindControls() {
/* Create two Binding objects for the first two TextBox 
  controls. The data-bound property for both controls 
  is the Text property. The data source is a DataSet 
  (ds). The data member is a navigation path in the form: 
  "TableName.ColumnName". */
    text1->DataBindings->Add(new Binding(S"Text", ds, S"customers.custName"));
    text2->DataBindings->Add(new Binding(S"Text", ds, S"customers.custID"));
   
   /* Bind the DateTimePicker control by adding a new Binding. 
  The data member of the DateTimePicker is a navigation path:
  TableName.RelationName.ColumnName string. */
    DateTimePicker1->DataBindings->Add(new Binding(S"Value", ds, S"customers.CustToOrders.OrderDate"));

   /* Add event delegates for the Parse and Format events to a 
  new Binding object, and add the object to the third 
  TextBox control's BindingsCollection. The delegates 
  must be added before adding the Binding to the 
  collection; otherwise, no formatting occurs until 
  the Current object of the BindingManagerBase for 
  the data source changes. */
    Binding __gc *b = new Binding (S"Text", ds, S"customers.custToOrders.OrderAmount");
    b->Parse += new ConvertEventHandler(this, &Form1::CurrencyStringToDecimal);
    b->Format += new ConvertEventHandler(this, &Form1::DecimalToCurrencyString);
    text3->DataBindings->Add(b);

   // Get the BindingManagerBase for the Customers table. 
    bmCustomers = this->BindingContext->Item[ds, S"Customers"];

   /* Get the BindingManagerBase for the Orders table using the 
  RelationName. */ 
    bmOrders = this->BindingContext->get_Item(ds, S"customers.CustToOrders");

   /* Bind the fourth TextBox control's Text property to the
   third control's Text property. */
    text4->DataBindings->Add(S"Text", text3, S"Text");
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

BindingContext メンバ | System.Windows.Forms 名前空間 | BindingManagerBase | Binding | BindingsCollection