Add category attribute to PropertyDescriptor

I have a set of custom PropertyDescriptor properties that I want to add categories to so they appear more organized in the PropertyGrid. I want each PropertyDescriptor type to be included in a specific category.

I tried to use TypeDescriptor.AddAttributes () to add attributes to the existing PropertyDescriptor, but no category attribute was added.

CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });

      

I've also tried using TypeDescriptor.AddAttributes () in the constructor for one of my PropertyDescriptors as shown below. But that doesn't work either.

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 });
}

      

I would rather not waste time on the details of why I do what I do. But in the above example, IntrinsicPropertyDef is a class that defines a property, including name, display name, and type. Therefore propDef.Attributes includes DisplayNameAttribute.

IntrinsicPropertyDef can be rendered with two different custom PropertyDescriptors IntrinsicPropertyDescriptor and InferedIntrinsicPropertyDescriptor. Each IntrinsicPropertyDescriptor must have a category attribute of the "Internal Properties" category, and each InferedIntrinsicPropertyDescriptor must have a category attribute "Inferred Intrinsic Properties".

+1


source to share


1 answer


I believe you can simply override Category

:

public override string Category { get {return "Foo";}}

      

For other scenarios; in general with a custom one PropertyDescriptor

, you specify the attributes in the constructor. You will need to expand the argument Attribute[]

to include CategoryAttribute

. If you need any handling, you can use the static method - untested:

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"))
{...}

      




Also - note that in order to use the PropertyDescriptor

system must find this ... permission rules:

  • for PropertyGrid

    , TypeConverter

    provides properties, defaults for properties for an instance (below)
  • for instance:
    • ICustomTypeDescriptor

      marked
    • otherwise it checks the registered TypeDescriptionProvider

      for the instance or type
    • different reflection is used
  • for type:
    • it checks the registered TypeDescriptionProvider

      type
    • different reflection is used
  • for lists:
    • IListSource

      the list is checked and resolved (processing continues)
    • ITypedList

      verified
    • otherwise, the list type is checked for an indexer with no objects - i.e. public SomeType this[int index] {get;}

      • if found, then the properties for the type SomeType

        are used as defined above
    • otherwise, if the list is not empty, the properties of the first instance ( list[0]

      ) are used as defined above
    • otherwise the metadata is not available
+4


source







All Articles