如何以编程方式对 Xceed PropertyGrid 中的类别进行排序?

How can you programmatically order categories in the Xceed PropertyGrid?

所示,我想要实现的是在 Xceed PropertyGrid 控件中对类别进行排序。

如该示例所示(复制此处以供参考),您可以在编译时通过向 class 添加属性来指定此信息,就像这样...

[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; }
}

它会像这样出现在 PropertyGrid 中...

不过我想做的是在运行时设置 CategoryOrderAttribute 值。这是我正在尝试的方法,但它不起作用...

// 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)
);

正如我所说,这不起作用,类别仍然按字母顺序显示。知道为什么这不起作用吗?

原来源代码中有两个错误。第一,它们没有覆盖 CategoryOrderAttribute 中的 TypeID,第二,它们没有使用 TypeDescriptor.GetAttributes。我已经提交了两个错误...

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