BindingContext 构造函数

定义

初始化 BindingContext 类的新实例。Initializes a new instance of the BindingContext class.

public:
 BindingContext();
public BindingContext ();
Public Sub New ()

示例

下面的代码示例创建两个新的 BindingContext 对象,并将每个对象分配给 GroupBox 控件的 BindingContext 属性。The following code example creates two new BindingContext objects and assigns each object to the BindingContext property of a GroupBox control. GroupBox1 包含 TextBox1GroupBox2 包含 TextBox2 (通过使用 AddRange 类的 Control.ControlCollection 方法来完成)。GroupBox1 contains TextBox1, and GroupBox2 contains TextBox2 (which is accomplished by using the AddRange method of the Control.ControlCollection class). 然后,该示例将 Binding 对象添加到这两个 TextBox 控件,并将每个对象绑定到相同的数据源和数据成员。The example then adds Binding objects to the two TextBox controls, binding each to the same data source and data member. 该示例还显示了两个事件处理程序,它们使用 GroupBox 控件中的 BindingContext 来设置不同 BindingManagerBase 对象上的 Position 属性。The example also shows two event handlers that use the BindingContext from the GroupBox controls to set the Position property on different BindingManagerBase objects.

void BindControls()
{
   System::Windows::Forms::BindingContext^ bcG1 = gcnew System::Windows::Forms::BindingContext;
   System::Windows::Forms::BindingContext^ bcG2 = gcnew System::Windows::Forms::BindingContext;
   groupBox1->BindingContext = bcG1;
   groupBox2->BindingContext = bcG2;
   textBox1->DataBindings->Add( "Text", ds, "Customers.CustName" );
   textBox2->DataBindings->Add( "Text", ds, "Customers.CustName" );
}

void Button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   groupBox1->BindingContext[ds, "Customers"]->Position = groupBox1->BindingContext[ds, "Customers"]->Position + 1;
}

void Button2_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   groupBox2->BindingContext[ds, "Customers"]->Position = groupBox2->BindingContext[ds, "Customers"]->Position + 1;
}

private void BindControls()
{
   BindingContext bcG1 = new BindingContext();
   BindingContext bcG2 = new BindingContext();

   groupBox1.BindingContext = bcG1;
   groupBox2.BindingContext = bcG2;

   textBox1.DataBindings.Add("Text", ds, "Customers.CustName");
   textBox2.DataBindings.Add("Text", ds, "Customers.CustName");
}

private void Button1_Click(object sender, EventArgs e)
{
   groupBox1.BindingContext[ds, "Customers"].Position += 1;         
}

private void Button2_Click(object sender, EventArgs e)
{
   groupBox2.BindingContext[ds, "Customers"].Position += 1;
}
Private Sub BindControls()
    Dim bcG1 As New BindingContext()
    Dim bcG2 As New BindingContext()
       
    groupBox1.BindingContext = bcG1
    groupBox2.BindingContext = bcG2
       
    textBox1.DataBindings.Add("Text", ds, "Customers.CustName")
    textBox2.DataBindings.Add("Text", ds, "Customers.CustName")
End Sub    
   
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    groupBox1.BindingContext(ds, "Customers").Position += 1
End Sub    
   
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    groupBox2.BindingContext(ds, "Customers").Position += 1
End Sub

注解

如果要为同一数据源具有多个 BindingManagerBase 实例,请创建新的 BindingContext 并将其设置为继承自 Control 类的对象的 BindingContext 属性。If you want to have multiple BindingManagerBase instances for the same data source, create a new BindingContext and set it to the BindingContext property of an object that inherits from the Control class. 例如,如果有两个 BindingManagerBase 对象(来自两个不同的 BindingContext 对象),则可以将每个 BindingManagerBasePosition 属性设置为不同的值。For example, if you have two BindingManagerBase objects (from two different BindingContext objects), you can set the Position properties of each BindingManagerBase to different values. 这将导致每个数据绑定控件集显示同一个数据源中的不同值。This causes each set of data-bound controls to display different values from the same data source.

适用于

另请参阅