Control.BindingContext 屬性

定義

取得或設定控制項的 BindingContext

C#
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.BindingContext BindingContext { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.BindingContext? BindingContext { get; set; }

屬性值

BindingContext

控制項的 BindingContext

實作

屬性

範例

下列程式碼範例會建立四 Binding 個 物件,將五個控制項和四 TextBox 個控制項系結至數個 DateTimePicker 資料來源。 BindingContext接著會用來取得每個資料來源的 BindingManagerBase

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");
}

備註

BindingContextControl 是用來傳回 由 所包含之所有資料繫結控制項的單 ControlBindingManagerBase 。 會 BindingManagerBase 讓系結至相同資料來源的所有控制項保持同步。 例如,設定 PositionBindingManagerBase 屬性會指定基礎清單中所有資料繫結控制項指向的專案。

如需建立新的 BindingContext 並將它指派給 BindingContext 屬性的詳細資訊,請參閱 BindingContext

給繼承者的注意事項

在衍生類別中覆 BindingContext 寫屬性時,請使用基類的 BindingContext 屬性來擴充基底實作。 否則,您必須提供所有實作。 您不需要同時覆寫 get 屬性的 BindingContextset 存取子;您可以視需要只覆寫一個。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另請參閱