次の方法で共有


Control.BindingContext プロパティ

コントロールの BindingContext を取得または設定します。

Public Overridable Property BindingContext As BindingContext
[C#]
public virtual BindingContext BindingContext {get; set;}
[C++]
public: __property virtual BindingContext* get_BindingContext();public: __property virtual void set_BindingContext(BindingContext*);
[JScript]
public function get BindingContext() : BindingContext;public function set BindingContext(BindingContext);

プロパティ値

コントロールの BindingContext

解説

ControlBindingContext オブジェクトを使用して、 Control に含まれるすべてのデータ連結コントロールの単一の BindingManagerBase オブジェクトを返します。 BindingManagerBase オブジェクトは、同期をとっている同一のデータ ソースにバインドされた、すべてのコントロールを維持します。たとえば、 BindingManagerBasePosition プロパティを設定して、すべてのデータ連結コントロールが指している、基になるリストの項目を指定します。

新しい BindingContext の作成方法とそれを BindingContext プロパティに割り当てる方法の詳細については、 BindingContext のトピックを参照してください。

継承時の注意: 派生クラスで BindingContext プロパティをオーバーライドする場合は、基本クラスの BindingContext プロパティを使用して、基本の実装を拡張します。それ以外の場合は、すべての実装を提供する必要があります。 BindingContext プロパティの get アクセサと set アクセサの両方をオーバーライドする必要はありません。必要に応じて 1 つだけオーバーライドする場合はあります。

使用例

[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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | BindingContextChanged | Binding | BindingManagerBase