WPF ComboxBox中的枚举(具有本地化名称) [英] Enum in WPF ComboxBox with localized names
问题描述
我有一个列出枚举的组合框.
I have a ComboBox listing an Enum.
enum StatusEnum {
Open = 1, Closed = 2, InProgress = 3
}
<ComboBox ItemsSource="{Binding StatusList}"
SelectedItem="{Binding SelectedStatus}" />
我想用英语显示枚举值的本地化名称
I want to display localized names for the enum values in English
Open
Closed
In Progress
而且还提供德语(以及将来的其他语言)
but also in German (and other languages in the future)
Offen
Geschlossen
In Arbeit
在我的ViewModel中使用
In my ViewModel using
public IEnumerable<StatusEnum> StatusList
{
get
{
return Enum.GetValues(typeof(StatusEnum)).Cast<StatusEnum>();
}
}
仅让我获取代码中的枚举的名称,而不获取翻译后的枚举的名称.
only gets me the names of the enum in the code and not the translated ones.
我已经进行了常规本地化,并且可以使用以下方式访问它们:
I have general localization in place and can access them using i.e.
Resources.Strings.InProgress
这使我获得了当前语言的翻译.
which gets me the translation for the current language.
如何自动绑定本地化?
推荐答案
这是简单的Enum
转换后的字符串转换器的示例.
It's an example of the simple Enum
to translated string converter.
public sealed class EnumToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{ return null; }
return Resources.ResourceManager.GetString(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string str = (string)value;
foreach (object enumValue in Enum.GetValues(targetType))
{
if (str == Resources.ResourceManager.GetString(enumValue.ToString()))
{ return enumValue; }
}
throw new ArgumentException(null, "value");
}
}
此外,您还需要一个MarkupExtension
来提供值:
Also you need a MarkupExtension
which will provide values:
public sealed class EnumerateExtension : MarkupExtension
{
public Type Type { get; set; }
public EnumerateExtension(Type type)
{
this.Type = type;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
string[] names = Enum.GetNames(Type);
string[] values = new string[names.Length];
for (int i = 0; i < names.Length; i++)
{ values[i] = Resources.ResourceManager.GetString(names[i]); }
return values;
}
}
用法:
<ComboBox ItemsSource="{local:Enumerate {x:Type local:StatusEnum}}"
SelectedItem="{Binding SelectedStatus, Converter={StaticResource EnumToStringConverter}}" />
编辑:您可以进行更复杂的值转换器和标记扩展. EnumToStringConverter
可以使用DescriptionAttribute
来获取翻译后的字符串.并且EnumerateExtension
可以使用TypeConverter.GetStandardValues()
和一个转换器.这样就可以获取指定类型的标准值(不仅是Enum
s),还可以将它们转换为字符串或其他形式,具体取决于转换器.
You can make a more complex value converter and markup extension. The EnumToStringConverter
can use DescriptionAttribute
's to get the translated strings. And the EnumerateExtension
can use TypeConverter.GetStandardValues()
and a converter. This allows to get standard values of the specified type (not only Enum
s) and convert them to strings or something another depending on the converter.
示例:
<ComboBox ItemsSource="{local:Enumerate {x:Type sg:CultureInfo}, Converter={StaticResource CultureToNameConverter}}"
SelectedItem="{Binding SelectedCulture, Converter={StaticResource CultureToNameConverter}}" />
编辑:上述更为复杂的解决方案现已发布在 GitHub 上
The more complex solution described above is published on GitHub now.
这篇关于WPF ComboxBox中的枚举(具有本地化名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!