How can you programmatically order categories in the Xceed PropertyGrid?

对着背影说爱祢 提交于 2019-12-11 04:49:09

问题


As shown in the example here, what I'm trying to achieve is ordering categories in the Xceed PropertyGrid control.

As that example shows (copied here for reference), you can specify this information at compile-time by adding attributes to the class, like so...

[CategoryOrder("General", 1)]
[CategoryOrder("Advanced", 2)]
[CategoryOrder("Other", 3)]
public class MyClass {
    [Category("General")]
    public string Property1 { get; set; }
    [Category("Advanced")]
    public int Property2 { get; set; }
    [Category("Other")]
    public double Property3 { get; set; }
    [Category("General")]
    public string Property4 { get; set; }
    [Category("Advanced")]
    public int Property5 { get; set; }
    [Category("Other")]
    public double Property6 { get; set; }
}

And it would appear in the PropertyGrid like this...

What I'm trying to do however is set the CategoryOrderAttribute values at runtime. Here's what I'm trying, but it's not working...

// Note: This gets executed *prior* to assignment to the PropertyGrid
TypeDescriptor.AddAttributes(typeof(MyClass),
    new CategoryOrderAttribute("General", 1),
    new CategoryOrderAttribute("Advanced", 2),
    new CategoryOrderAttribute("Other", 3)
);

Like I said, this doesn't work and the categories still appear in alphabetical order. Any idea why that doesn't work?


回答1:


Turns out there are two bugs in the source. One, they aren't overriding TypeID in CategoryOrderAttribute, and two, they aren't using TypeDescriptor.GetAttributes. I've submitted bugs for both...

https://github.com/xceedsoftware/wpftoolkit/issues/1522



来源:https://stackoverflow.com/questions/57550862/how-can-you-programmatically-order-categories-in-the-xceed-propertygrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!