添加了数据库相关了类,和枚举类型相关的类,并且将枚举类型绑定到前段
This commit is contained in:
35
ValueConverts/EnumDescriptionConverter.cs
Normal file
35
ValueConverts/EnumDescriptionConverter.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
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());
|
||||
|
||||
DescriptionAttribute[] attributes =
|
||||
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
|
||||
if (attributes != null && attributes.Length > 0)
|
||||
return attributes[0].Description;
|
||||
else
|
||||
return enumObj.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user