简体   繁体   English

如何在用户控件上公开组合框数据绑定

[英]How to expose combobox Databindings on a usercontrol

I have a combobox on a usercontrol . 我在usercontrol上有一个组合框 I can expose the datasource however I cant expose the actual bindings. 我可以公开数据源,但是我不能公开实际的绑定。

If you add a normal combobox to a form and go to the databindings property you can choose selected value, text etc. 如果将普通的组合框添加到表单并转到databindings属性,则可以选择选定的值,文本等。

After this is chosen the designer automatically creates a 选择此选项后,设计师将自动创建一个

combobox.databindings.add("SelectedValue", datasource, columname, true));

How can I expose a combobox on a user control so that it has the above behavior 如何在用户控件上公开组合框 ,使其具有上述行为

It's probably not considered best practice to expose your controls like this since after all, part of the point of using a UserControl is to hide the details of the child controls. 像这样公开您的控件可能不被认为是最佳实践,因为毕竟,使用UserControl的部分目的是隐藏子控件的详细信息。

Try exposing the control on the UserControl as a property: 尝试将控件作为属性公开在UserControl上:

public partial class UserControl1 : UserControl {
  public UserControl1() {
    InitializeComponent();
  }

  [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  public ComboBox ComboBox {
    get {
      return this.comboBox1;
    }
  }
}

If you are only interested in the control's DataBindings, then try to just expose that information: 如果您只对控件的DataBindings感兴趣,则尝试仅公开该信息:

public partial class UserControl1 : UserControl {
  public UserControl1() {
    InitializeComponent();
  }

  [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  public ControlBindingsCollection ComboDataBindings {
    get {
      return this.comboBox1.DataBindings;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM