How can I customize category sorting on a PropertyGrid?

前端 未结 4 624
深忆病人
深忆病人 2021-02-07 01:49

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

If I set either of the following...

propertyGrid.PropertySort = PropertySor         


        
相关标签:
4条回答
  • 2021-02-07 02:14

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

    0 讨论(0)
  • 2021-02-07 02:17

    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.

    0 讨论(0)
  • 2021-02-07 02:33

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

    0 讨论(0)
  • 2021-02-07 02:38

    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.

    0 讨论(0)
提交回复
热议问题