[C#]public: BindingContext();
备注
当希望有同一数据源的多个 BindingManagerBase 实例时,请创建一个新的 BindingContext,并将它设置为从 Control 继承的对象的 BindingContext 属性。
例如,如果您有两个 BindingManagerBase 对象(来自两个不同的 BindingContext 对象),可以将每个 BindingManagerBase 的 Position 属性设置为不同的值,这些值导致每组数据绑定控件显示同一数据源中的不同值。
示例
[ C#] 下面的示例创建两个新的 BindingContext 对象,并将每个对象分配给一个 GroupBox 控件的 BindingContext 属性。GroupBox1 包含 TextBox1,而 GroupBox2 包含 TextBox2(通过使用 Control.ControlCollection 的 AddRange 方法来完成)。然后该示例将 Binding 对象添加到这两个 TextBox 控件,将各个控件绑定到相同的数据源和数据成员。示例还展示了两个事件处理程序,它们使用 GroupBox 控件中的 BindingContext 设置不同 BindingManagerBase 对象上的 Position 属性。
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;
}
本文介绍了C#中的BindingContext(),强调了其在处理多个BindingManagerBase实例时的作用,允许不同控件显示同一数据源的不同值。通过示例展示了如何创建新的BindingContext对象并将其分配给GroupBox控件,以及如何使用Button事件处理程序改变Position属性,从而改变TextBox中显示的数据。
1102

被折叠的 条评论
为什么被折叠?



