Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum type editor now uses and favors existing enum entry descriptions. #7

Merged
merged 1 commit into from
May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}