如何自定义分类中的类别排序 PropertyGrid?

如果我设置以下任意一项...

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

...那么类别将按字母顺序排列。(“按字母顺序”似乎适用于每个类别中的属性。)如果我使用 PropertySort.NoSort, ,我失去了分类。

我正在填充我的 PropertyGridSelectObject, ,这很简单:

this.propertyGrid1.SelectedObject = options;

options 是具有适当修饰属性的类的实例:

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

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

我有六个类别的几十个属性。

有什么方法可以调整类别排序顺序,同时保持易用性 SelectedObject?

有帮助吗?

解决方案

如果您的意思是希望按特定(非字母)方式排序类别,那么不 - 我认为您不能这样做。您可能想尝试 VisualHint - 我希望它确实有这个(因为你可以抓住更多控制权。)

其他提示

我认为这个链接很有用http://bytes.com/groups/net-c/21​​4456-q-ordering-sorting-category-text-propertygrid

我不相信有办法做到这一点。我唯一能 发现表明您可以这样做的是 PropertySort 财产如果设置为 "无",则会显示属性 的顺序排列。您可能会 可以在对象和 属性网格,然后不仅会返回正确的 顺序,而是按您希望的顺序排列类别的属性 他们在...

就像@Marc Gravel在他的回答中所说,框架中没有任何内容允许这种行为。任何解决方案都是黑客攻击。话虽如此,您可以使用@Shahab在他的答案中提出的解决方案作为解决方法,但不是真的表明你有意维护你的代码。所以我认为你能做的最好的事情就是创建一个自定义的 Attribute ,它继承自 CategoryAttribute 来为你处理这个过程:

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))
    {
    }
}

然后你可以这样使用它

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

只需确保将 PropertyGrid UseCompatibletextRendering 属性设置为 true ,以便为您删除不可打印的字符和< code> PropertySort 设置为 Categorized CategorizedAlphabetical ,你应该很高兴。

上面描述的'\ t'技巧的一个小变化,我只是用回车符('\ r')来尝试它。它似乎可以工作并避免由选项卡引入的额外空间引起的工具提示问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top