/////////////////////////////////////////////////////////////////////////////// //Copyright (c) 2016, 北京探索者软件公司 //All rights reserved. //文件名称: EnumToStringConvert.cs //文件描述: 枚举转换成控件显示 //创 建 者: xls //创建日期: 2018-1-17 //版 本 号:1.0.0.0 //////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace FWindSoft.Wpf { /// /// 枚举转换成字符串 /// public class EnumToStringConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Type type = value.GetType(); if (!type.IsEnum) return value; string display = value.ToString(); FieldInfo fieldInfo = type.GetField(display); if (fieldInfo == null) return display; object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if ( attributes.Length > 0) { DescriptionAttribute info = attributes[0] as DescriptionAttribute; if (info != null) { display = info.Description; } } return display; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }