我有一组自定义的PropertyDescriptor,我还想添加类别,以便它们在PropertyGrid中以更有组织的方式显示。我希望每种类型的PropertyDescriptor都归入一个特定的类别。
我尝试使用TypeDescriptor.AddAttributes()向现有类别添加属性,但没有添加PropertyDescriptor属性。
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });我还尝试在一个PropertyDescriptors的构造函数中使用TypeDescriptor.AddAttributes(),如下所示。但它也不起作用。
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.Attributes包含了DisplayNameAttribute。
IntrinsicPropertyDef可以与两个不同的自定义PropertyDescriptors IntrinsicPropertyDescriptor和InferedIntrinsicPropertyDescriptor一起显示。每个IntrinsicPropertyDescriptor都应该有一个类别属性“固有属性”,每个InferedIntrinsicPropertyDescriptor都应该有一个类别属性“推断出的固有属性”。
发布于 2009-05-05 19:40:19
我相信你可以直接重写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,反射提供的属性默认为实例的属性(如下所示),而实例的反射为:ICustomTypeDescriptor is checkedTypeConverter is typeTypeDescriptionProvider
类型的
的反射检查注册的TypeDescriptionProvider:
列表的处理类型:检查
IListSource并将其解析为列表(processing continues)ITypedList is checkedpublic SomeType this[int index] {get;} public SomeType this[int index] {get;}
- otherwise, if the list is not empty, the properties of the first instance (`list[0]`) are used, as defined above
- otherwise, metadata is not available
https://stackoverflow.com/questions/826437
复制相似问题