BindingContext クラス

定義

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

public ref class BindingContext : System::Collections::ICollection
public class BindingContext : System.Collections.ICollection
type BindingContext = class
    interface ICollection
    interface IEnumerable
Public Class BindingContext
Implements ICollection
継承
BindingContext
実装

次のコード例では、4 つの Binding オブジェクトを作成して、5 つのコントロール (および DateTimePicker 4 つの TextBox コントロール) を複数のデータ ソースにバインドします。 BindingContext次に、 を使用して各データ ソースの をBindingManagerBase取得します。

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( gcnew Binding( "Text",ds,"customers.custName" ) );
   text2->DataBindings->Add( gcnew 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( gcnew 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 = gcnew Binding( "Text",ds,"customers.custToOrders.OrderAmount" );
   b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
   b->Format += gcnew ConvertEventHandler( this, &Form1::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" );
}
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");
}
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

注釈

各 Windows フォームには、フォームのオブジェクトを管理するオブジェクトがBindingManagerBase少なくとも 1 つBindingContext存在します。 BindingManagerBaseクラスは抽象であるため、プロパティの戻り値のItem[]型は または PropertyManagerですCurrencyManager。 データ ソースが (オブジェクトのリストではなく) 1 つのプロパティのみを返すことができるオブジェクトの場合、 Type は です PropertyManager。 たとえば、データ ソースとして を TextBox 指定すると、 PropertyManager が返されます。 一方、データ ソースが または IBindingListCurrencyManager を実装IListするオブジェクトである場合は、 が返されます。

Windows フォーム上のデータ ソースごとに、 または PropertyManagerが 1 つありますCurrencyManager。 Windows フォームには複数のデータ ソースが関連付けられている可能性があるため、 を使用すると、 BindingContext データ ソースに関連付けられている特定 CurrencyManager のデータ ソースを取得できます。

Note

プロパティを Item[] 使用する場合、 BindingContext は、まだ存在しない場合に新しい BindingManagerBase を作成します。 これは、返されたオブジェクトが目的のリスト (またはリスト) を管理しない可能性があるため、混乱を招く可能性があります。 無効な BindingManagerBaseが返されないようにするには、 メソッドを Contains 使用して、意図した BindingManagerBase が既に存在するかどうかを判断します。

、、または TabControlなどのコンテナー コントロールをGroupBoxPanel使用してデータ バインド コントロールを含める場合は、そのコンテナー コントロールとそのコントロールに対して を作成BindingContextできます。 その後、フォームの各部分を独自 BindingManagerBaseの で管理できます。 同じデータ ソースに対して BindingContext 複数 BindingManagerBase のオブジェクトを作成する方法の詳細については、コンストラクターを参照してください。

コントロールを TextBox フォームに追加し、それをデータセット内のテーブルの列にバインドすると、コントロールはそのフォームの と BindingContext 通信します。 次に、 は BindingContext、そのデータ関連付けに固有の と話 CurrencyManager し合います。 の CurrencyManagerプロパティに対してクエリをPosition実行すると、そのコントロールのバインドに関する現在のレコードがTextBox報告されます。 次のコード例では、TextBoxコントロールは、そのフォームの を介してBindingContextデータセットのdataSet1テーブルのCustomers列にバインドFirstNameされています。

TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")  
textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");  
textBox1->DataBindings->Add("Text", dataSet1, "Customers.FirstName");  

フォームに 2 つ目TextBoxのコントロール (TextBox2) を追加し、同じデータセット内のテーブルのCustomers列にバインドLastNameできます。 BindingContextは最初のバインディング (TextBox1 から ) をCustomers.FirstName認識しているため、両方のテキスト ボックスが同じデータセット (DataSet1) にバインドされているため、同じ CurrencyManagerを使用します。

TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")  
textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");  
textBox2->DataBindings->Add("Text", dataSet1, "Customers.LastName");  

別のデータセットにバインドTextBox2すると、 によって 2 つ目CurrencyManagerBindingContext が作成および管理されます。

プロパティと DisplayMember プロパティを設定DataSourceする方法について一貫性を持つ必要があります。それ以外の場合は、 BindingContext によって同じデータセットに対して複数の通貨マネージャーが作成され、エラーが発生します。 次のコード例は、プロパティとそれに関連付けられている BindingContext オブジェクトを設定するいくつかの方法を示しています。 コード全体で一貫性がある限り、次のいずれかのメソッドを使用してプロパティを設定できます。

ComboBox1.DataSource = DataSet1  
ComboBox1.DisplayMember = "Customers.FirstName"  
Me.BindingContext(dataSet1, "Customers").Position = 1  
comboBox1.DataSource = DataSet1;  
comboBox1.DisplayMember = "Customers.FirstName";  
this.BindingContext[dataSet1, "Customers"].Position = 1;  
comboBox1->DataSource = dataSet1;  
comboBox1->DisplayMember = "Customers.FirstName";  
this->BindingContext->get_Item(dataSet1, "Customers")->Position = 1;  
ComboBox1.DataSource = DataSet1.Customers  
ComboBox1.DisplayMember = "FirstName"  
Me.BindingContext(dataSet1.Customers).Position = 1  
comboBox1.DataSource = DataSet1.Customers;  
comboBox1.DisplayMember = "FirstName";  
this.BindingContext[dataSet1.Customers].Position = 1;  
comboBox1->DataSource = dataSet1->Customers;  
comboBox1->DisplayMember = "FirstName";  
this->BindingContext->get_Item(dataSet1->Customers)->Position = 1;  

Note

ほとんどのWindows フォームアプリケーションは、 BindingSourceを介してバインドされます。 コンポーネントは BindingSourceCurrencyManager カプセル化し、プログラミング インターフェイスを CurrencyManager 公開します。 バインディングに を BindingSource 使用する場合は、 によって BindingSource 公開されるメンバーを使用して、 を経由するのではなく、 Position"通貨" (つまり) を操作する BindingContext必要があります。

コンストラクター

BindingContext()

BindingContext クラスの新しいインスタンスを初期化します。

プロパティ

IsReadOnly

コレクションが読み取り専用かどうかを示す値を取得します。

Item[Object, String]

指定したデータ ソースおよびデータ メンバーに関連付けられた BindingManagerBase を取得します。

Item[Object]

指定したデータ ソースに関連付けられた BindingManagerBase を取得します。

メソッド

Add(Object, BindingManagerBase)

特定のデータ ソースに関連付けられた BindingManagerBase をコレクションに追加します。

AddCore(Object, BindingManagerBase)

特定のデータ ソースに関連付けられた BindingManagerBase をコレクションに追加します。

Clear()

すべての BindingManagerBase オブジェクトのコレクションを削除します。

ClearCore()

コレクションを空にします。

Contains(Object)

指定したデータ ソースに関連付けられた BindingContextBindingManagerBase に格納されているかどうかを示す値を取得します。

Contains(Object, String)

指定したデータ ソースおよびデータ メンバーに関連付けられた BindingContextBindingManagerBase に格納されているかどうかを示す値を取得します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnCollectionChanged(CollectionChangeEventArgs)

CollectionChanged イベントを発生させます。

Remove(Object)

指定したデータ ソースに関連付けられた BindingManagerBase を削除します。

RemoveCore(Object)

指定したデータ ソースに関連付けられた BindingManagerBase を削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
UpdateBinding(BindingContext, Binding)

Binding を新しい BindingContext に関連付けます。

イベント

CollectionChanged

処理されると、常に NotImplementedException が発生します。

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

コレクションのインデックスを開始位置として、指定した配列にコレクション要素をコピーします。

ICollection.Count

CurrencyManager によって管理される BindingContext オブジェクトの合計数を取得します。

ICollection.IsSynchronized

コレクションの同期がとられているかどうかを示す値を取得します。

ICollection.SyncRoot

同期 (スレッド セーフティ) に使用するオブジェクトを取得します。

IEnumerable.GetEnumerator()

コレクションの列挙子を取得します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください