按枚举类型对datagrid列进行排序

时间:2013-04-01 23:56:21

标签: c# wpf sorting datagrid enums

我的DataGrid中有一列显示了一些枚举值。我将SortMemberPath设置为我的属性,该属性由这些枚举值表示。但是,当单击列标题时,它不会对我的枚举进行排序,因为我没有指定如何对此类型进行排序。这是一个枚举,所以我无法完全覆盖<运营商。有什么想法吗?

更新: 如果可以的话,我正试图避开第二处房产。我只能设置SortMemberPath =“MyEnumProperty”。我无法告诉它如何解释这个价值。

_colDefs["Subtype"] = new DataGridTextColumn()
{       
    Binding = new Binding("") { Converter = new SubtypeConverter() },
    SortMemberPath = "Subtype"
};

我的财产看起来像这样:

public SubtypeEnum Subtype { get; set; }

2 个答案:

答案 0 :(得分:1)

你可以通过强硬转换将枚举转换为int:

(int)MyEnum.EnumValue

如果你可以为你的数据结构添加一个假的属性,只是简单地将它的枚举值重命名为int,你应该能够通过该属性对它进行排序,即使它不可见。

答案 1 :(得分:1)

尝试在绑定中指定属性名称:

_colDefs["Subtype"] = new DataGridTextColumn()
{       
    Binding = new Binding("Subtype"),
    SortMemberPath = "Subtype"
};

你甚至不再需要转换器(如果它只是转换为字符串)。

相关问题