我正在使用PropertyGrid來編輯集合。與收集的對象被定義爲以下:如何在PropertyGrid自定義集合編輯器中的「添加」按鈕下拉列表中自定義名稱
class ObjWithCollection
{
[Editor(typeof(MyCustomCollectionEditor),typeof(UITypeEditor))]
public List<ItemBase> collection { get; set; } = new List<ItemBase>();//ItemBase is abstract
}
該集合包含兩種類型,從衍生的對象ItemBase類:項目1和項目2。定義如下這些類:
public abstract class ItemBase
{
public string Name { get; set; }
public ItemBase() { }
public ItemBase(string name) { Name = name; }
}
public class Item1:ItemBase
{
public Item1():base("Item 1 name"){}
}
[DisplayName("item2 test display name")]
public class Item2:ItemBase
{
public Item2() : base("item 2 name") { }
}
爲了能夠通過編輯器增加新項目的收集,我也定義了自定義集合編輯器類並覆蓋CreateNewItemTypes列出適合收藏各類:
class MyCustomCollectionEditor : CollectionEditor
{
public MyCustomCollectionEditor(Type type) : base(type){}
protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(Item1), typeof(Item2) };
}
}
然後,我結合我的自定義編輯器將ObjWithCollection.collection財產與編輯 attibute(見ObjWithCollection.collection定義)。
這工作正常,我能夠編輯我的集合,包括添加新項目。 Add按鈕有一個下拉菜單,允許用戶選擇要添加的元素的類型。 editor window http://i.share.pho.to/31d50d09_o.png
但在添加按鈕下拉列表稱爲「項目1」和「項目2」的項目,我不能改變這個名字。我試過DisplayName屬性,ToString重寫,但沒有運氣。
所以,的qustion是如何,我可以輸出爲的元素自定義名稱添加按鈕菜單。