wpf 绑定到 xaml 中枚举的显示名称属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28671828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:58:33  来源:igfitidea点击:

Binding to display name attribute of enum in xaml

c#wpfxamlenums

提问by mosquito87

I have the following enum:

我有以下枚举:

public enum ViewMode
{
    [Display(Name = "Neu")]
    New,
    [Display(Name = "Bearbeiten")]
    Edit,
    [Display(Name = "Suchen")]
    Search
}

I'm using xaml and databinding to show the enum in my window:

我正在使用 xaml 和数据绑定在我的窗口中显示枚举:

<Label Content="{Binding CurrentViewModel.ViewMode}" Grid.Column="2" VerticalContentAlignment="Bottom" Height="43" HorizontalContentAlignment="Right"/>

But this doesn't show the display name attribute. How can I do so?

但这不会显示显示名称属性。我怎么能这样做?

In my viewModel I can get the display name attribute by using an extension method:

在我的 viewModel 中,我可以使用扩展方法获取显示名称属性:

public static class EnumHelper
{
    /// <summary>
    /// Gets an attribute on an enum field value
    /// </summary>
    /// <typeparam name="T">The type of the attribute you want to retrieve</typeparam>
    /// <param name="enumVal">The enum value</param>
    /// <returns>The attribute of type T that exists on the enum value</returns>
    public static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attribute
    {
        var type = enumVal.GetType();
        var memInfo = type.GetMember(enumVal.ToString());
        var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
        return (attributes.Length > 0) ? (T)attributes[0] : null;
    }
}

Usage is string desc = myEnumVariable.GetAttributeOfType<DescriptionAttribute>().Description;. However, this doesn't help in XAML.

用法是string desc = myEnumVariable.GetAttributeOfType<DescriptionAttribute>().Description;。但是,这在 XAML 中没有帮助。

回答by Grx70

Create a class implementing the System.Windows.Data.IValueConverterinterface and specify it as the binding's converter. Optionally, for easier usage, you can create a "provider" class implementing System.Windows.Markup.MarkupExtension(actually you can do both with just one class). Your end result could resemble this example:

创建一个实现System.Windows.Data.IValueConverter接口的类并将其指定为绑定的转换器。或者,为了更容易使用,您可以创建一个“提供者”类实现System.Windows.Markup.MarkupExtension(实际上您可以只用一个类来实现这两个)。您的最终结果可能类似于此示例:

public class MyConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((Enum)value).GetAttributeOfType<DisplayAttribute>().Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

And then in XAML:

然后在 XAML 中:

<Label Content="{Binding CurrentViewModel.ViewMode, Converter={local:MyConverter}}" Grid.Column="2" VerticalContentAlignment="Bottom" Height="43" HorizontalContentAlignment="Right"/>