forked from HandyOrg/HandyControl
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SamirKharchi/Improve-Enum-Display-By-Using…
…-Description Enum type editor now uses and favors existing enum entry descriptions.
- Loading branch information
Showing
1 changed file
with
54 additions
and
5 deletions.
There are no files selected for viewing
59 changes: 54 additions & 5 deletions
59
src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/EnumPropertyEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> GetEnumDescription(Type enumType) | ||
{ | ||
var values = Enum.GetValues(enumType); | ||
var enumDescs = new List<string>(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<KeyValuePair<Enum, string>> cache; | ||
|
||
public EnumDescriptionConverter(Type enumType) | ||
{ | ||
var values = Enum.GetValues(enumType); | ||
|
||
cache = new List<KeyValuePair<Enum, string>>(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, string>((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; | ||
} | ||
} | ||
} |