EnumToStringConverter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //Copyright (c) 2016, 北京探索者软件公司
  4. //All rights reserved.
  5. //文件名称: EnumToStringConvert.cs
  6. //文件描述: 枚举转换成控件显示
  7. //创 建 者: xls
  8. //创建日期: 2018-1-17
  9. //版 本 号:1.0.0.0
  10. ////////////////////////////////////////////////////////////////////////////////
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Data;
  19. namespace FWindSoft.Wpf
  20. {
  21. /// <summary>
  22. /// 枚举转换成字符串
  23. /// </summary>
  24. public class EnumToStringConverter:IValueConverter
  25. {
  26. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27. {
  28. Type type = value.GetType();
  29. if (!type.IsEnum)
  30. return value;
  31. string display = value.ToString();
  32. FieldInfo fieldInfo = type.GetField(display);
  33. if (fieldInfo == null)
  34. return display;
  35. object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  36. if ( attributes.Length > 0)
  37. {
  38. DescriptionAttribute info = attributes[0] as DescriptionAttribute;
  39. if (info != null)
  40. {
  41. display = info.Description;
  42. }
  43. }
  44. return display;
  45. }
  46. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. }
  51. }