2011-05-12 88 views
2

我有一個包含組合框的用戶控件。我想能夠編輯組合框的Items屬性,但我不知道如何做到這一點。我已經嘗試將Items屬性添加到我的用戶控件類中,但是我不確定在Visual Studio的屬性菜單中設置屬性時返回的值是什麼。我有屬性的設置是這樣的:暴露usercontrol中的組合框項目屬性

[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); 
     } 
    } 

回答

4

裹在屬性的用戶控件的ComboBox的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; 
    } 
} 

該屬性中的EditorAttribute指定用於更改設計器中屬性的UI元素。

+0

這是否只適用於您在代碼中設置項目?我試圖添加視覺工作室設計器視圖的屬性菜單中的項目,它沒有奏效。當我運行應用程序時,組合框沒有項目。 – MBU 2011-05-12 23:49:28

+0

@MBU編輯我的答案 - 找到你如何做你需要做的。非常便利。 – 2011-05-13 19:22:17

0

嘗試增加兩種方法添加和刪除ComboBox項目:

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

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