如何在“添加”中自定义名称PropertyGrid自定义集合编辑器中的按钮下拉列表 [英] How to customize names in "Add" button dropdown in the PropertyGrid custom collection editor
问题描述
我正在使用 PropertyGrid 编辑集合。具有集合的对象的定义如下:
I'm using PropertyGrid to edit a collection. An object with the collection is defined as following:
class ObjWithCollection
{
[Editor(typeof(MyCustomCollectionEditor),typeof(UITypeEditor))]
public List<ItemBase> collection { get; set; } = new List<ItemBase>();//ItemBase is abstract
}
集合包含从 ItemBase 类派生的两种类型的对象: Item1 和 Item2 。这些类的定义如下:
The collection contains the objects of two types, derived from ItemBase class: Item1 and Item2. These classes defined as following:
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 以列出所有适合集合的类型:
To be able to add new items to the collection via the editor, I also defined the custom collection editor class and override CreateNewItemTypes to list all types suitable for collection:
class MyCustomCollectionEditor : CollectionEditor
{
public MyCustomCollectionEditor(Type type) : base(type){}
protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(Item1), typeof(Item2) };
}
}
然后我将自定义编辑器绑定到带有 Editor 属性的ObjWithCollection.collection 属性(请参见 ObjWithCollection.collection 定义)。
Then I bind my custom editor to the ObjWithCollection.collection property with Editor attibute (see ObjWithCollection.collection definition).
很好,我可以编辑我的收藏,包括添加新商品。 添加按钮具有一个下拉菜单,允许用户选择要添加的元素的类型。
编辑器窗口http://i.share.pho.to/31d50d09_o.png
This works fine and I able to edit my collection, including the adding of the new items. The Add button has a dropdown which allows user to select the type of element to add. editor window http://i.share.pho.to/31d50d09_o.png
但是在添加按钮下拉列表中,名为 Item1 和 Item2 的项目无法更改此名称。我尝试使用 DisplayName 属性, ToString 覆盖,但没有运气。
But in the Add button dropdown the items called "Item1" and "Item2" and I can't change this names. I tried DisplayName attribute, ToString override, but no luck.
因此,问题是如何输出添加按钮菜单的元素的自定义名称。
So, the qustion is how to I can output the custom names for the elements of the Add button menu.
推荐答案
我认为这不可能直接从属性网格的代码中实现。但是,您可以使用 TypeDelegator 来欺骗系统并强制其使用例如DisplayName属性代替默认情况下使用的类型的Name属性。
I don't think this is possible directly from the property grid's code. However, you can use a TypeDelegator to trick the system and force it to use for example your DisplayName attribute in lieu of the type's Name property it uses by default.
1)创建一个自定义TypeDelegator,如下所示:
1) create a custom TypeDelegator, like this:
class MyTypeDelegator : TypeDelegator
{
public MyTypeDelegator(Type delegatingType)
: base(delegatingType)
{
}
public override string Name
{
get
{
var dna = (DisplayNameAttribute)typeImpl.GetCustomAttribute(typeof(DisplayNameAttribute));
return dna != null ? dna.DisplayName : typeImpl.Name;
}
}
}
2)修改CreateNewItemTypes()像这样:
2) modify CreateNewItemTypes() like this:
protected override Type[] CreateNewItemTypes()
{
return new Type[] { new MyTypeDelegator(typeof(Item1)), new MyTypeDelegator(typeof(Item2)) };
}
现在,您应该看到显示名称,而不是菜单中的名称。
Now, you should see the display names instead of the name in the menu.
这篇关于如何在“添加”中自定义名称PropertyGrid自定义集合编辑器中的按钮下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!