Прочетете на английски Редактиране

Споделяне чрез


BindingContext Constructor

Definition

Initializes a new instance of the BindingContext class.

C#
public BindingContext();

Examples

The following code example creates two new BindingContext objects and assigns each object to the BindingContext property of a GroupBox control. GroupBox1 contains TextBox1, and GroupBox2 contains TextBox2 (which is accomplished by using the AddRange method of the Control.ControlCollection class). The example then adds Binding objects to the two TextBox controls, binding each to the same data source and data member. The example also shows two event handlers that use the BindingContext from the GroupBox controls to set the Position property on different BindingManagerBase objects.

C#
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;
}

Remarks

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. 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.

Applies to

Продукт Версии
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also