[英]How can I copy the databinding from one control to another?
我有一个带有很多用户输入控件的表单; 它们中的大多数是可选的,并且由于我无法控制的原因,所需的元素分散在表单周围。 我被要求添加一个按钮来打开第二个表单(此后称为ChildForm),该按钮链接到原始表单(此后称为ParentForm),并且仅具有ParentForm所需的控件。
我希望将ChildForm中的控件链接到与其在ParentForm中的相应控件相同的数据源。 我想以编程方式在循环中创建此链接,以便以后对ParentForm进行更改时,无需手动编辑ChildForm控件的数据绑定。
我尝试了ChildControl.DataBindings.Add(ParentControl.DataBindings[0]);
但是我得到一个dataBinding belongs to another BindingsCollection
在运行时dataBinding belongs to another BindingsCollection
ArgumentException。
如何将新控件与现有控件绑定到DataTable的同一列上,而无需为每个控件手动进行操作?
如果您的Binding
很简单(没有注册任何Format
和Parse
事件处理程序),则可以执行以下浅表克隆:
public void CloneBinding(Control control, Binding bind){
Binding bind = new Binding(bind.PropertyName, bind.DataSource, bind.BindingMemberInfo.BindingMember);
control.DataBindings.Add(bind);
}
//Use it
CloneBinding(ChildControl, ParentControl.DataBindings[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.