BindingContext 建構函式

定義

初始化 BindingContext 類別的新執行個體。Initializes a new instance of the BindingContext class.

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

範例

下列程式碼範例會建立兩個新 BindingContext 的物件,並將每個物件指派給 BindingContext 控制項的屬性 GroupBoxThe following code example creates two new BindingContext objects and assigns each object to the BindingContext property of a GroupBox control. GroupBox1 包含 TextBox1 ,而且 GroupBox2 包含 TextBox2 (使用 AddRange) 類別的方法來完成 Control.ControlCollectionGroupBox1 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. 此範例也會顯示兩個事件處理常式,這些處理常式會使用 BindingContext 來自 GroupBox 控制項的來設定 Position 不同物件上的屬性 BindingManagerBaseThe 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 並將它設定為 BindingContext 繼承自類別之物件的屬性 ControlIf 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 物件) ,您可以將 Position 每個物件的屬性設定 BindingManagerBase 為不同的值。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.

適用於

另請參閱