在用户控件中公开组合框项属性

本文关键字:组合 属性 用户 控件 | 更新日期: 2023-09-27 17:56:31

>我有一个包含组合框的用户控件。 我希望能够编辑组合框的 Items 属性,但我不确定如何做到这一点。 我尝试将 Items 属性添加到我的用户控件类,但我不确定在 Visual Studio 的属性菜单中设置属性时返回的值是什么。我有这样的属性设置:

[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design",
    "System.Drawing.Design.UITypeEditor, System.Drawing")]
    public ComboBox.ObjectCollection Items
    {
        get
        {
            return this.comboBox.Items;
        }
        set
        {
            this.comboBox.Items.Add(value);
        }
    }  

在用户控件中公开组合框项属性

用户控件的组合框的 Items 属性包装在如下所示的属性中:

[Description("The items in the UserControl's ComboBox."), 
 DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
 Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
public System.Windows.Forms.ComboBox.ObjectCollection MyItems {
    get { 
        return comboBox1.Items; 
    }
}
属性

中的编辑器属性指定用于在设计器中更改属性的 UI 元素。

尝试添加两种方法来添加和删除ComboBox项:

public void AddItem(Object item)
{
    this.comboBox.Items.Add(item);
}
public void RemoveItem(Object item)
{
    this.comboBox.Items.Remove(item);
}