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

实现

属性

示例

下面的代码示例创建四Binding个对象,用于将五个控件(a 和四个DateTimePickerTextBox控件)绑定到多个数据源。 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 用于为 包含Control的所有数据绑定控件返回一BindingManagerBase个 。 使 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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅