BindingContext 构造函数

初始化 BindingContext 类的新实例。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub New
用法
Dim instance As New BindingContext
public BindingContext ()
public:
BindingContext ()
public BindingContext ()
public function BindingContext ()

备注

如果想要同一数据源有多个 BindingManagerBase 实例,创建一个新 BindingContext 并将其设置为一个从 Control 类派生的对象的 BindingContext 属性。例如,如果有两个 BindingManagerBase 对象(来自两个不同的 BindingContext 对象),可以将每个 BindingManagerBasePosition 属性设置为不同的值。这会导致每组数据绑定控件从相同的数据源显示不同的值。

示例

下面的代码示例创建两个新的 BindingContext 对象,并将每个对象分配给一个 GroupBox 控件的 BindingContext 属性。GroupBox1 包含 TextBox1,而 GroupBox2 包含 TextBox2(通过使用 Control.ControlCollection 类的 AddRange 方法来实现)。然后该示例将 Binding 对象添加到这两个 TextBox 控件,将各个控件绑定到相同的数据源和数据成员。示例还展示了两个事件处理程序,它们使用 GroupBox 控件中的 BindingContext 设置不同 BindingManagerBase 对象上的 Position 属性。

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
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;
}
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.set_BindingContext(bcG1);
    groupBox2.set_BindingContext(bcG2);
    textBox1.get_DataBindings().Add("Text", ds, "Customers.CustName");
    textBox2.get_DataBindings().Add("Text", ds, "Customers.CustName");
} //BindControls

private void button1_Click(Object sender, EventArgs e)
{
    groupBox1.get_BindingContext().get_Item(ds, "Customers").set_Position
        (groupBox1.get_BindingContext().get_Item(ds, "Customers").
        get_Position() + 1);
} //button1_Click

private void button2_Click(Object sender, EventArgs e)
{
    groupBox2.get_BindingContext().get_Item(ds, "Customers").set_Position
        (groupBox2.get_BindingContext().get_Item(ds, "Customers").
        get_Position() + 1);
} //button2_Click
private function BindControls()
{
   var bcG1 : System.Windows.Forms.BindingContext = new System.Windows.Forms.BindingContext;
   var bcG2 : System.Windows.Forms.BindingContext = new System.Windows.Forms.BindingContext;

   button1.BindingContext = bcG1;
   button2.BindingContext = bcG2;

   textBox1.DataBindings.Add(new Binding
      ("Text", ds, "customers.custName"));
   textBox2.DataBindings.Add(new Binding
      ("Text", ds, "customers.custName"));

   // Get the BindingManagerBase for the Customers table. 
   bmCustomers = this.BindingContext [ds, "Customers"];
}

private function Button1_Click(sender, e : EventArgs)
{
   bmCustomers.Position += 1;  
}

private function Button2_Click(sender, e : EventArgs)
{
   bmCustomers.Position -= 1;
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

BindingContext 类
BindingContext 成员
System.Windows.Forms 命名空间
BindingManagerBase