我有一个问题要问。我有一个枚举,它在运行时显示在 UI 中。它具有三个值。
enum ExpiryOptions
{
Never,
After,
On
}
现在从 userControl 加载它的显示 Never、After、on。
<ComboBox x:Name="accessCombo" Margin="5" Height="25" Width="80"
ItemsSource="{Binding Source={StaticResource ResourceKey=expiryEnum},
Converter={StaticResource enumtoLocalizeConverter}}"/>
用英文很好,但问题是,如果将软件用作本地化设置,则会出现相同的字符串。而不是任何本地化的字符串。
在转换器中我写了这样的代码
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
ExpiryOption[] myEnum = value; // This myEnum is having all the enum options.
// Now what shall I write here
//if I write a code like this
if(myEnum[0] == Properties.Resources.Never)
return Properties.Resources.Never;
else if(myEnum[1] == Properties.Resources.After)
return Properties.Resources.After;
else if(myEnum[2] == Properties.Resources.On)
return Properties.Resources.On;
}
然后 UI 中的枚举填充 N E V E R(垂直)在英语语言设置中。显然第一个字符串匹配并填充 Never 其他两个选项丢失。非常需要任何建议和帮助。
您总是从转换器返回第一个枚举值,即字符串值 Never
,它是 char 数组,因此您在 comboBox 中看到一个项目作为单个 char。
相反,您应该返回字符串列表:
List<string> descriptions = new List<string>();
foreach(ExpiryOption option in myEnum)
{
if(option == Properties.Resources.Never)
descriptions.Add(Properties.Resources.Never);
else if(option == Properties.Resources.After)
descriptions.Add(Properties.Resources.After);
else if(option == Properties.Resources.On)
descriptions.Add(Properties.Resources.On);
}
return descriptions;
我是一名优秀的程序员,十分优秀!