BindingContext 构造函数

定义

初始化 BindingContext 类的新实例。

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

示例

下面的代码示例创建两个新 BindingContext 对象,并将每个对象分配给 BindingContext 控件的属性 GroupBoxGroupBox1``TextBox1包含并使用GroupBox2 TextBox2 类) 的方法Control.ControlCollection完成AddRange的 (。 然后,该示例将对象添加到 BindingTextBox 个控件,每个控件绑定到同一数据源和数据成员。 该示例还显示了两个事件处理程序,这些事件处理程序使用 BindingContext 控件 GroupBox 设置 Position 不同 BindingManagerBase 对象的属性。

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 实例并将其设置为 BindingContextControl 类继承的对象的属性。 例如,如果两 BindingManagerBase 个对象 (两个不同的 BindingContext 对象) ,则可以将 Position 每个 BindingManagerBase 对象的属性设置为不同的值。 这会导致每个数据绑定控件集显示来自同一数据源的不同值。

适用于

另请参阅