【问题标题】:How can I copy the databinding from one control to another?如何将数据绑定从一个控件复制到另一个控件?
【发布时间】:2013-09-06 16:46:22
【问题描述】:

我有一个包含很多用户输入控件的表单;其中大多数是可选的,并且由于我无法控制的原因,所需的元素分散在表单周围。我被要求添加一个按钮,用于打开第二个表单(以下称为 ChildForm),该表单链接到原始表单(以下称为 ParentForm),并且仅具有 ParentForm 所需的控件。

我希望 ChildForm 中的控件与 ParentForm 中的相应控件链接到相同的数据源。我想在循环中以编程方式创建此链接,以便以后对 ParentForm 的更改不需要手动编辑 ChildForm 控件的数据绑定。

我尝试了ChildControl.DataBindings.Add(ParentControl.DataBindings[0]);,但在运行时得到dataBinding belongs to another BindingsCollection ArgumentException。

如何将新控件与现有控件绑定到 DataTable 的同一列,而无需为每个控件手动执行此操作?

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    如果您的Binding 很简单(没有注册任何FormatParse 事件处理程序),您可以像这样进行浅层克隆:

    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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 2011-11-14
      • 2011-12-14
      • 1970-01-01
      • 2012-10-16
      • 2018-01-25
      • 2012-03-24
      • 2015-02-21
      相关资源
      最近更新 更多