我有一组自定义的 PropertyDescriptor,我也想添加类别,以便它们在 PropertyGrid 中以更有条理的方式显示。我希望每种类型的 PropertyDescriptor 进入一个特定的类别。
我尝试使用 TypeDescriptor.AddAttributes() 将属性添加到现有的 PropertyDescriptor,但未添加类别属性。
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });
我还尝试在构造函数中为我的一个 PropertyDescriptor 使用 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。
可以使用两个不同的自定义 PropertyDescriptor IntrinsicPropertyDescriptor 和 InferedIntrinsicPropertyDescriptor 来显示 IntrinsicPropertyDef。每个 IntrinsicPropertyDescriptor 都应该有一个类别属性“Intrinsic Properties”,每个 InferedIntrinsicPropertyDescriptor 都应该有一个类别属性“Inferred Intrinsic Properties”。