seyounth.hyosung.ty/Seyounth.Hyosung/ViewConverter/EnumDescriptionConverter.cs

24 lines
819 B
C#
Raw Normal View History

2025-03-17 22:17:28 +08:00
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;
namespace Seyounth.Hyosung.ViewConverter;
public class EnumDescriptionConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value == null) return null;
var field = value.GetType().GetField(value.ToString() ?? string.Empty);
if (field == null) return null;
var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length == 0 ? value.ToString() : ((DescriptionAttribute)attributes[0]).Description;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}