Question

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?

Was it helpful?

Solution

If you mean that you want the categories sorted in a specific (non-alphabetical) way, then no - I don't think you can do that. You might want to try VisualHint - I expect it does have this (since you can seize a lot more control).

OTHER TIPS

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...

Like @Marc Gravel said in his answer, there's nothing in the framework that allows this behaviour. Any solution will be a hack. With that said, you can use the solution suggested by @Shahab in his answer as a work-around but that doesn't really indicate your intention to anyone maintaining your code. So I think the best you can do is create a custom Attribute which inherits from CategoryAttribute to handle the process for you:

public class CustomSortedCategoryAttribute : CategoryAttribute
{
    private const char NonPrintableChar = '\t';

    public CustomSortedCategoryAttribute(   string category,
                                            ushort categoryPos,
                                            ushort totalCategories)
        : base(category.PadLeft(category.Length + (totalCategories - categoryPos),
                    CustomSortedCategoryAttribute.NonPrintableChar))
    {
    }
}

Then you can use it as such

[CustomSortedCategory("Z Category",1,2)]
public string ZProperty {set;get;}
[CustomSortedCategory("A Category",2,2)]
public string AProperty {set;get;}

Just make sure you set the PropertyGrid's UseCompatibletextRendering property to true to strip out the non-printable characters for you and the PropertySort set to Categorized or CategorizedAlphabetical and you should be good to go.

A small variation on the '\t' trick described above, I just tried it with carriage return characters ('\r') instead. It seems to work and avoids the tooltip problem caused by the extra space introduced by a tab.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top