添加一个类别属性PropertyDescriptor
-
05-07-2019 - |
题
我有一套定义PropertyDescriptor,我想添加类别太多,所以他们显示在一个更有组织的方式在PropertyGrid.我希望每个类型的PropertyDescriptor进入一个特定的类别。
我已经试过使用TypeDescriptor.AddAttributes()添加的属性给现有PropertyDescriptor,但该类别的属性是不加入。
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });
我们还试图使用TypeDescriptor.AddAttributes()中的构造对我的一个PropertyDescriptors如下所示。但是它不工作。
public IntrinsicPropertyDescriptor(IntrinsicPropertyDef propDef): base(propDef.Key, propDef.Attributes)
{
this._type = propDef.Type;
this._key = propDef.Key;
this._readOnly = propDef.ReadOnly;
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
TypeDescriptor.AddAttributes(this, new Attribute[] { intrinsicPropertyCategory });
}
我宁愿不要花时间去详细的为什么我做什么我做的。但在上面的例子IntrinsicPropertyDef是一类定义了财产,包括名称、显示的姓名和类型。所以propDef.属性,包括DisplayNameAttribute.
一个IntrinsicPropertyDef可以显示有两种不同的定义PropertyDescriptors IntrinsicPropertyDescriptor,并InferedIntrinsicPropertyDescriptor.每IntrinsicPropertyDescriptor应该有一个类别特性"的内在特性",并且每InferedIntrinsicPropertyDescriptor应该有一个类别属性"推断出来的内在属性"。
解决方案
我 相信 你可以替代的 Category
:
public override string Category { get {return "Foo";}}
其他方案;在普通用定义 PropertyDescriptor
, 你指定的属性的构造。你会需要扩大 Attribute[]
参数包括 CategoryAttribute
.如果你需要做任何处理,可以使用一种静态的方法-未经检验:
static Attribute[] AddCategory(Attribute[] attributes, string category) {
Array.Resize(ref attributes, attributes.Length + 1);
attributes[attributes.Length - 1] = new CategoryAttribute(category);
return attributes;
}
public IntrinsicPropertyDescriptor(IntrinsicPropertyDef propDef)
: base(propDef.Key, AddCategory(propDef.Attributes, "Foo"))
{...}
还注意到,对于一个 PropertyDescriptor
要被使用的系统必须找到它...该决议规则是:
- 对于
PropertyGrid
, ,TypeConverter
提供的性质,违约的性质对于一个实例(见下文) - 对于一个实例:
ICustomTypeDescriptor
检查- 否则它检查的一个注册的
TypeDescriptionProvider
为实例或类型 - 否则使用反射
- 对于一个类型:
- 它检查的一个注册的
TypeDescriptionProvider
的类型 - 否则使用反射
- 它检查的一个注册的
- 用于清单:
IListSource
是的检查和得到解决的一个列表(继续处理)ITypedList
检查- 否则,列表类型的是检查非目indexer-即
public SomeType this[int index] {get;}
- 如果发现,那么性质的类型
SomeType
使用,如上述定义
- 如果发现,那么性质的类型
- 否则,如果名单不是空的,性质的第一个实例(
list[0]
)是使用,如上述定义 - 否则,元数据不可用