是,通過傳遞的值到轉換器中的時間這將是一個string
作爲枚舉(EnumConverter)爲GetStandardValues
(即Enum.GetValues()
)的默認類型轉換器返回的字段以字符串枚舉的。
解決此問題的最佳方法是編寫自定義類型轉換器來裝飾您的Enums。幸運的是,您不是第一個需要這樣做的人,請參閱下面的代碼示例。
public class EnumTypeConverter : EnumConverter
{
public EnumTypeConverter()
: base(typeof(Enum))
{
}
public EnumTypeConverter(Type type)
: base(type)
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || TypeDescriptor.GetConverter(typeof(Enum)).CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return GetEnumValue(EnumType, (string)value);
if (value is Enum)
return GetEnumDescription((Enum)value);
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Enum && destinationType == typeof(string))
return GetEnumDescription((Enum)value);
if (value is string && destinationType == typeof(string))
return GetEnumDescription(EnumType, (string)value);
return base.ConvertTo(context, culture, value, destinationType);
}
public static bool GetIsEnumBrowsable(Enum value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var attributes = (BrowsableAttribute[])fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);
return !(attributes.Length > 0) || attributes[0].Browsable;
}
public static string GetEnumDescription(Enum value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
}
public static string GetEnumDescription(Type value, string name)
{
var fieldInfo = value.GetField(name);
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : name;
}
public static object GetEnumValue(Type value, string description)
{
var fields = value.GetFields();
foreach (var fieldInfo in fields)
{
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0 && attributes[0].Description == description)
return fieldInfo.GetValue(fieldInfo.Name);
if (fieldInfo.Name == description)
return fieldInfo.GetValue(fieldInfo.Name);
}
return description;
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return base.GetStandardValues(context);
}
}
使用
[TypeConverter(typeof(EnumTypeConverter))]
public enum UserTypes : int
{
[Browsable(false)]
Unkown,
[Description("Local")]
LocalUser,
[Description("Network")]
NetworkUser,
[Description("Restricted")]
RestrictedUser
}
正如你所看到的,上面枚舉,我們使用了Description
屬性來裝點每個字段與用戶朋友的描述,並已覆蓋了類型轉換器首先尋找這個屬性。
不是100%,但獲得此與您的代碼工作,你還需要改變你的MarkupExtension
爲以下(注:我沒有測試過這一點,所以在你做一些工作是必需的)。
[MarkupExtensionReturnType(typeof (IEnumerable))]
public class EnumValuesExtension : MarkupExtension {
public EnumValuesExtension() {}
public EnumValuesExtension(Type enumType)
{
this.EnumType = enumType;
}
[ConstructorArgument("enumType")]
public Type EnumType { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.EnumType == null)
throw new ArgumentException("The enum type is not set");
var converter = TypeDescriptor.GetConverter(this.EnumType);
if (converter != null && converter.GetStandardValuesSupported(this.EnumType))
return converter.GetStandardValues(this.EnumType);
return Enum.GetValues(this.EnumType);
}
}
而且,我只是做了有限的本地化應用程序,但是我相信這是最好的和最易維護的方法,這將能夠充分利用現有的.NET本地化工具(如衛星組件)
請顯示'UserTypesToStringConverter'的代碼 –
您可以發佈'UserTypesToStringConverter'的相關代碼。 – Dennis
您是否嘗試過使用衛星程序集使用WPF本地化?那麼你不需要在代碼中進行任何本地化工作。 http://msdn.microsoft。com/en-us/library/ms788718.aspx - 您可以使用您的枚舉的ToString值爲這些標籤控件提供一個UID來解決此特定問題。 –