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

时间:2019-08-19 05:21:28

标签: c# wpf wpftoolkit propertygrid xceed

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

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

[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中出现...

enter image description here

但是,我要尝试在运行时设置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)
);

就像我说的那样,这行不通,并且类别仍按字母顺序显示。知道为什么这行不通吗?

1 个答案:

答案 0 :(得分:1)

结果证明源代码中有两个错误。一是它们没有覆盖CategoryOrderAttribute中的TypeID,二是它们没有使用TypeDescriptor.GetAttributes。我已经提交了两个错误……

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