更新
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
public class EnumDescriptionConvert : Collection<String>, IValueConverter
|
||||
{
|
||||
private Type type;
|
||||
|
||||
private IDictionary<Object, Object> valueToNameMap;
|
||||
|
||||
private IDictionary<Object, Object> nameToValueMap;
|
||||
|
||||
public Type Type
|
||||
{
|
||||
get { return this.type; }
|
||||
set
|
||||
{
|
||||
if (!value.IsEnum)
|
||||
throw new ArgumentException("类型不是枚举类", "value");
|
||||
this.type = value;
|
||||
Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
|
||||
{
|
||||
return this.valueToNameMap[value];
|
||||
}
|
||||
|
||||
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
|
||||
{
|
||||
return this.nameToValueMap[value];
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
this.valueToNameMap = this.type
|
||||
.GetFields(BindingFlags.Static | BindingFlags.Public)
|
||||
.ToDictionary(fi => fi.GetValue(null), GetDescription);
|
||||
this.nameToValueMap = this.valueToNameMap
|
||||
.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
|
||||
Clear();
|
||||
foreach (String name in this.nameToValueMap.Keys)
|
||||
Add(name);
|
||||
}
|
||||
|
||||
private static Object GetDescription(FieldInfo fieldInfo)
|
||||
{
|
||||
var descriptionAttribute =
|
||||
(DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));
|
||||
return descriptionAttribute != null ? descriptionAttribute.Description : fieldInfo.Name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user