BindingContext 建構函式

定義

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

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

範例

下列程式碼範例會建立兩BindingContext個新的物件,並將BindingContext每個物件GroupBox指派給控制項的屬性。The following code example creates two new BindingContext objects and assigns each object to the BindingContext property of a GroupBox control. GroupBox1包含TextBox1、和GroupBox2包含AddRange (這Control.ControlCollection是使用類別的方法來完成)。 TextBox2GroupBox1 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不同BindingManagerBase物件的屬性。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新的BindingContext ,並將它設定為繼承自Control類別之物件的屬性。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的物件),您可以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.

適用於

另請參閱