2025-06-20 18:53:29 +08:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.ValueConverts;
|
|
|
|
|
|
|
|
|
|
|
|
public class EnumDescriptionConverter : IValueConverter
|
|
|
|
|
|
{
|
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
|
return DependencyProperty.UnsetValue;
|
|
|
|
|
|
|
|
|
|
|
|
return GetEnumDescription(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetEnumDescription(object enumObj)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fi = enumObj.GetType().GetField(enumObj.ToString());
|
|
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
var attributes =
|
2025-06-20 18:53:29 +08:00
|
|
|
|
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
|
|
|
|
|
|
|
|
|
|
if (attributes != null && attributes.Length > 0)
|
|
|
|
|
|
return attributes[0].Description;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
return enumObj.ToString();
|
2025-06-20 18:53:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|