How can I customize category sorting on a PropertyGrid?

Garth Kidd picture Garth Kidd · May 5, 2009 · Viewed 17.5k times · Source

How can I customize the sorting of categories in a PropertyGrid?

If I set either of the following...

propertyGrid.PropertySort = PropertySort.Categorized;
propertyGrid.PropertySort = PropertySort.CategorizedAlphabetical;

... then the categories will be alphabetized. ("Alphabetical" would seem to apply to the properties within each category.) If I use PropertySort.NoSort, I lose categorization.

I'm populating my PropertyGrid with SelectObject, which is pretty easy:

this.propertyGrid1.SelectedObject = options;

options is an instance of a class with suitably decorated properties:

    [CategoryAttribute("Category Title"),
    DisplayName("Property Name"),
    Browsable(true),
    ReadOnly(false),
    BindableAttribute(true),
    DesignOnly(false),
    DescriptionAttribute("...")]
    public bool PropertyName {
        get {
            // ...
        }

        set {
            // ...
            this.OnPropertyChanged("PropertyName");
        }
    }

I have a few dozen properties in half a dozen categories.

Is there some way I can adjust the category sort order while preserving my ease of use with SelectedObject?

Answer

Shahab picture Shahab · Jun 15, 2009

I think this link is useful http://bytes.com/groups/net-c/214456-q-ordering-sorting-category-text-propertygrid

I don't believe there is a way to do this. The only thing that I could find that indicates you might be able to do this is the PropertySort property. If you set it to none, it says that the properties are displayed in the order that they are received from the type descriptor. You might be able to create a proxy type descriptor between your object and the propertygrid, which would then return not only the properties in the correct order, but the properties with the categories in the order that you want them in...