LOGIN    REGISTER    YOUR CART

homepage
VisualHint's blog
Apr14

Properties and categories sorting

This is a concern often seen in forums or newsgroups. With the Microsoft PropertyGrid, sorting the properties means having to derive from ICustomTypeDescriptor or TypeDescriptionProvider to return the properties in a specific order. Sorting the categories is simply impossible. I think I have to remedy this problem.

First, don't forget the first mode of operation of SPG: in this mode you can dynamically add and remove categories and properties, so by doing that you control the order.

In the more traditionnal way (reflection), we need something very easy for the developer. I was thinking to 2 possibilities. Let me know what you think about it:

  • Use a new attribute to decorate a property:

    [Category("Personal")]
    [PropertyRank(1)]
    public int MyData { set; get; }
    

    This new tag, "PropertyRank", could be used for categories and properties.

  • Give access to the collection of sibling child properties/categories for a node and let the user reorder them. It could give something like this:

    PropertyEnumerator categoryEnum = propGrid.FindProperty("Personal");
    PropertyEnumerator children = MySort(categoryEnum.Property.Children);
    propGrid.Reorder(children);
    

Of course the usual way with ICustomTypeDescriptor must continue to be available.