WPF通用枚举值转枚举名称转化器
xmlns:Enums="clr-namespace:XXX.Models.Enums;assembly=XXX"
界面绑定转化器>>转化器参数为枚举类型(Enums:为枚举所在的,命名空间)
<TextBlock x:Name="tbkCardName" Text="{Binding Path=Type ,Converter={StaticResource EnumValueToEnumNameConverter} , ConverterParameter={x:Type Enums:VisitCardTypeEnum}}" ></TextBlock>
转化器
/// <summary>
/// 枚举值转枚举名称 (parameter传枚举类型)
/// </summary>
[ValueConversion(typeof(int), typeof(string))]
public class EnumValueToEnumNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (value == null) return DependencyProperty.UnsetValue;
Assembly assem = Assembly.GetExecutingAssembly();
Type type = assem.GetType(parameter.ToString());
var enumName = Enum.Parse(type, value.ToString());
return enumName;
}
catch (Exception e)
{
Logger.Error(nameof(TemplateTypeConverter), e);
return string.Empty;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
希望大佬们提出优化意见。转载请标明出处。