From c5b9c33c6d1c2420969379e6a2acf3a557b7b40d Mon Sep 17 00:00:00 2001 From: SKharchi Date: Tue, 11 May 2021 13:12:29 +0200 Subject: [PATCH] Enum type editor now uses and favors existing enum entry descriptions. --- .../Editors/EnumPropertyEditor.cs | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/EnumPropertyEditor.cs b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/EnumPropertyEditor.cs index 3187a0fdd..2d4469711 100644 --- a/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/EnumPropertyEditor.cs +++ b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/EnumPropertyEditor.cs @@ -1,17 +1,66 @@ using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.Linq; using System.Windows; using System.Windows.Controls.Primitives; +using System.Windows.Data; namespace HandyControl.Controls { public class EnumPropertyEditor : PropertyEditorBase { - public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox - { - IsEnabled = !propertyItem.IsReadOnly, - ItemsSource = Enum.GetValues(propertyItem.PropertyType) - }; + public override FrameworkElement CreateElement(PropertyItem propertyItem) => + new System.Windows.Controls.ComboBox + { + IsEnabled = !propertyItem.IsReadOnly, + ItemsSource = EnumPropertyEditor.GetEnumDescription(propertyItem.PropertyType) + }; public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty; + + protected override IValueConverter GetConverter(PropertyItem propertyItem) => new EnumDescriptionConverter(propertyItem.PropertyType); + + private static IEnumerable GetEnumDescription(Type enumType) + { + var values = Enum.GetValues(enumType); + var enumDescs = new List(values.Length); + + enumDescs.AddRange(from object value in values + select enumType.GetField(value.ToString()) + into field + let attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false) + select (attributes.Length == 0 ? field.Name : ((DescriptionAttribute) attributes[0]).Description)); + + return enumDescs; + } + + private class EnumDescriptionConverter : IValueConverter + { + private readonly List> cache; + + public EnumDescriptionConverter(Type enumType) + { + var values = Enum.GetValues(enumType); + + cache = new List>(values.Length); + + foreach (var value in values) + { + var field = enumType.GetField(value.ToString()); + var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false); + var fieldValue = (attributes.Length == 0 ? field.Name : ((DescriptionAttribute) attributes[0]).Description); + + cache.Add(new KeyValuePair((Enum) value, fieldValue)); + } + } + + object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) => + cache.First(x => x.Key.Equals((Enum) value)).Value; + + object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => + cache.First(x => x.Value == (string) value).Key; + } } }