如何在用户控件中设置设计视图项,以便多个子控件具有这些项

本文关键字:控件 用户 设置 视图 | 更新日期: 2023-09-27 18:35:27

我有一个UserControl,它内部有10个ComboBox。

我需要使我的父用户控件具有设计时 Items 属性,就像 ComboBox 通常具有的那样,它将设置所有子项 ComboBox。

组合框的项的设计时定义为:

//
// Summary:
//     Gets an object representing the collection of the items contained in this
//     System.Windows.Forms.ComboBox.
//
// Returns:
//     A System.Windows.Forms.ComboBox.ObjectCollection representing the items in
//     the System.Windows.Forms.ComboBox.
[MergableProperty(false)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
public ComboBox.ObjectCollection Items
{
    get;
}

所以这将起作用,除了这不会将内部项目应用于组合框元素。

我无法将它们分配给倍数,因为这些ComboBox.ObjectCollection项在创建时链接到特定的ComboBox。

有什么建议吗?

如何在用户控件中设置设计视图项,以便多个子控件具有这些项

您可以使用

BindingList<string>来实现此目的:

private BindingList<string> items = new BindingList<string>();
public UserControl1() {
  InitializeComponent();
  comboBox1.DataSource = new BindingSource(items, string.Empty);
  comboBox2.DataSource = new BindingSource(items, string.Empty);
}
[MergableProperty(false)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
public BindingList<string> Items {
  get { return items; }
}