【问题标题】:How to expose combobox Databindings on a usercontrol如何在用户控件上公开组合框数据绑定
【发布时间】:2014-04-10 15:04:51
【问题描述】:

我在 usercontrol 上有一个 combobox。我可以公开数据源,但不能公开实际绑定。

如果您将普通的组合框添加到表单并转到数据绑定属性,您可以选择选定的值、文本等。

选择此选项后,设计师会自动创建一个

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

如何在用户控件上公开一个组合框,使其具有上述行为

【问题讨论】:

  • 我可能没有抓住重点,但是在用户控件上有一个方法来设置数据绑定或通过usercontrol.combobox.databindings访问组合框有什么问题?
  • 我本来就有这个。它可以工作,但是看起来很笨重。我希望用户控件上的组合框像通常通过设计器一样工作

标签: c# winforms data-binding combobox


【解决方案1】:

这样公开控件可能不被认为是最佳实践,因为毕竟使用 UserControl 的部分目的是隐藏子控件的详细信息。

尝试将 UserControl 上的控件作为属性公开:

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

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

如果您只对控件的 DataBindings 感兴趣,请尝试只公开该信息:

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

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

【讨论】:

  • 给你的第二个例子加分 成功了!就这么简单
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2012-03-29
  • 1970-01-01
  • 2011-06-06
相关资源
最近更新 更多