如何在DataGrid中显示枚举的[描述]

时间:2014-04-01 12:55:37

标签: c# wpf enums

我正在构建一个WPF应用程序。

我的DataGrid显示包含

数据的行
ItemSrouce="{Bingding Accounts}"

在C#中

accountListViewModel.Accounts = entityService.Accounts; // each record is an instance of Account class

Account类有一个名为Genders的Enum属性。 Enum是这样的:

public enum MyEnum
{
    [Description("MyEnum1 Description")]
    MyEnum1,
    [Description("MyEnum2 Description")]
    MyEnum2,
    [Description("MyEnum3 Description")]
    MyEnum3
}

如何在WPF中 DataGrid 显示描述而不是枚举名称?

3 个答案:

答案 0 :(得分:3)

您可以为单元格绑定创建自定义value converter,并将其用于检索属性值的逻辑。

public class EnumDescriptionConverter : IValueConverter
{
    private string GetEnumDescription(Enum enumObj)
    {
        FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
        var descriptionAttr = fieldInfo
                                      .GetCustomAttributes(false)
                                      .OfType<DescriptionAttribute>()
                                      .Cast<DescriptionAttribute>()
                                      .SingleOrDefault();
        if (descriptionAttr == null)
        {
            return enumObj.ToString();
        }
        else
        {
            return descriptionAttr.Description;
        }
    }

    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Enum myEnum = (Enum)value;
        string description = GetEnumDescription(myEnum);
        return description;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Empty;
    }
}

答案 1 :(得分:0)

您应该使用Enum Extension类或Value Converter来执行此操作,

wpf-databinding-enum-with-nice-descriptions

答案 2 :(得分:0)

您真正要的是一个TypeConverter

对于DataGrid来说,仅对您的枚举成员上的EnumMemberDescription进行修饰似乎还不够。它还希望具有用[TypeConverter(typeof(YourConverter))]装饰的实际类型。 ComponentModel中存在一个EnumConverter,但是它不具备属性意识-只是'ToString()是枚举成员。

因此,我的建议是:

这可能是您的枚举:

[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum ComparisonOperators
{
    [Description("=")]
    [EnumMember(Value = "=")]
    Equal,

    [Description("<>")]
    [EnumMember(Value = "<>")]
    Unequal,

    [Description("<")]
    [EnumMember(Value = "<")]
    LessThan,

    [Description("<=")]
    [EnumMember(Value = "<=")]
    LessThanOrEqual,

    [Description(">")]
    [EnumMember(Value = ">")]
    GreaterThan,

    [Description(">=")]
    [EnumMember(Value = ">=")]
    GreaterThanOrEqual
}

然后,您需要提到的EnumDescriptionTypeConverter(请注意,它以相同的方式尊重EnumMember和Description属性):

    public class EnumDescriptionTypeConverter : EnumConverter
{
    public EnumDescriptionTypeConverter(Type type)
        : base(type)
    {
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            if (value != null)
            {
                FieldInfo fi = value.GetType().GetField(value.ToString());
                if (fi != null)
                {
                    var dattributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    var eattributes = (EnumMemberAttribute[])fi.GetCustomAttributes(typeof(EnumMemberAttribute), false);
                    return dattributes.Select(d => d.Description).Concat(eattributes.Select(e => e.Value))
                               .FirstOrDefault() ?? string.Empty;
                }
            }

            return string.Empty;
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }
}

然后,您只需将ViewModel绑定到DataGrid:

<DataGrid ItemsSource="{Binding Vm}" />

启用自动生成功能后,它将所有Enum成员显示为组合框。

示例VM:

public class MyVm : ViewModelBase
{
    [DisplayName("Number")]
    [Description("Provide a number")]
    public int OrdinaaryOrdinal {get; set;}

    [DisplayName("Compare operator")]
    [Description("to do")]
    public ComparisonOperators Operator {get; set;}
    ...

中提琴会按预期显示:

Grid running with that enum type.

资源: