Binding to display name attribute of enum in xaml Binding to display name attribute of enum in xaml wpf wpf

Binding to display name attribute of enum in xaml


Create a class implementing the System.Windows.Data.IValueConverter interface 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:

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:

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