using System; using System.ComponentModel; using System.Globalization; using System.Reflection; using System.Windows.Data; namespace MultiTerm.Wpf.ValueConverters; public class EnumDescriptionConverter : IValueConverter { private string GetEnumDescription(Enum enumObject) { FieldInfo fieldInfo = enumObject.GetType().GetField(enumObject.ToString()); object[] attributeArray = fieldInfo.GetCustomAttributes(false); if(attributeArray.Length == 0) { return enumObject.ToString(); } else { DescriptionAttribute attribute = attributeArray[0] as DescriptionAttribute; return attribute.Description; } } /// /// Convert from Data Source to Dependency Object type. /// Here => Enum type to object. /// /// /// /// /// /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Enum myEnum = (Enum)value; string description = this.GetEnumDescription(myEnum); return description; } /// /// Convert from Dependency Object type to Data Source type. /// Here => object to Enum type. /// /// /// /// /// /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { int returnValue = 0; if (parameter is Type) { returnValue = (int)Enum.Parse((Type)parameter, value.ToString()); } return returnValue; } }