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 Tango.SharedUI.Converters { /// /// Represents a binding converter for converting enum value to the associated description attribute if found. /// /// public class EnumToDescriptionConverter : IValueConverter { /// /// Converts a value. /// /// The value produced by the binding source. /// The type of the binding target property. /// The converter parameter to use. /// The culture to use in the converter. /// /// A converted value. If the method returns null, the valid null value is used. /// /// public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null) { return GetDescription(value, value.ToString()); } else { return null; } } public static string GetDescription(object enumValue, string defDesc) { FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString()); if (null != fi) { object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true); if (attrs != null && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return defDesc; } /// /// Converts a value. /// /// The value that is produced by the binding target. /// The type to convert to. /// The converter parameter to use. /// The culture to use in the converter. /// /// A converted value. If the method returns null, the valid null value is used. /// /// public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } } }