Files
qhmes/yy-admin-master/YY.Admin.Core/Converter/EnumDescriptionConverter.cs

26 lines
838 B
C#

using System.Globalization;
using System.Reflection;
using System.Windows.Data;
namespace YY.Admin.Core.Converter
{
public class EnumDescriptionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || value.ToString() == null) return string.Empty;
var field = value.GetType().GetField(value.ToString()!);
if (field == null) return value.ToString()!;
var attribute = field.GetCustomAttribute<DescriptionAttribute>();
return attribute?.Description ?? value.ToString()!;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}