How can I customize the sorting of categories in a PropertyGrid
?
If I set either of the following...
propertyGrid.PropertySort = PropertySor
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).
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.
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...
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.