gpt4 book ai didi

c# - 如何在 WPF 中将枚举转换为本地化枚举结构

转载 作者:太空宇宙 更新时间:2023-11-03 13:28:34 26 4
gpt4 key购买 nike

我有一个问题要问。我有一个枚举,它在运行时显示在 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;

关于c# - 如何在 WPF 中将枚举转换为本地化枚举结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21438282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com