gpt4 book ai didi

c# - 如何在 UserControl 中设置设计 View 项,以便多个子控件具有这些项

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:08 29 4
gpt4 key购买 nike

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

我需要这样做,以便我的父 UserControl 具有设计时 Items 属性,就像 ComboBox 通常具有的那样,然后将设置所有子 ComboBox。

ComboBox 的 Items 设计时的定义是:

//
// 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; }
}

关于c# - 如何在 UserControl 中设置设计 View 项,以便多个子控件具有这些项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495134/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com