我有一组自定义的PropertyDescriptor,我也想添加类别,因此它们以更有组织的方式显示在PropertyGrid中。我希望每种类型的PropertyDescriptor都能进入特定的类别。向PropertyDescriptor添加类别属性
我试过使用TypeDescriptor.AddAttributes()向现有PropertyDescriptor添加属性,但不添加category属性。
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应该有一个category属性“Intrinsic Properties”,并且每个InferedIntrinsicPropertyDescriptor都应该有一个category属性“Inferred Intrinsic Properties”。
是的,你建议的类别覆盖工作完美。我知道如何使用构造函数,但并不认为使用静态方法来简化代码,我必须将其作为参数传递给构造函数。谢谢! – 2009-05-05 21:28:18