码迷,mamicode.com
首页 > 编程语言 > 详细

最简单的WPF的ComboBox显示多语言的Enum

时间:2016-08-20 10:13:52      阅读:542      评论:0      收藏:0      [点我收藏+]

标签:

<UserControl.Resources>

<converters:AllParitiesToStringListConverter x:Key="AllParitiesToStringListConverter"/>
<converters:ParityToStringConverter x:Key="ParityToStringConverter"/>

</UserControl.Resources>

<StackPanel>
<Label Content="校验位" />
<ComboBox ItemsSource="{Binding AllParities, Converter={StaticResource AllParitiesToStringListConverter}}"
SelectedItem="{Binding PcComViewModel.Parity, Mode=TwoWay, Converter={StaticResource ParityToStringConverter}}">
</ComboBox>
</StackPanel>

[ValueConversion(typeof(Parity), typeof(string))]
public class ParityToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Parity parity = (Parity)value;

if (CultureInfo.CurrentCulture.Name == "en-US")
{
if (parity == Parity.Odd)
{
return "Odd";
}
else if (parity == Parity.Even)
{
return "Even";
}
else
{
return "None";
}
}
else
{
if (parity == Parity.Odd)
{
return "奇";
}
else if (parity == Parity.Even)
{
return "偶";
}
else
{
return "无";
}
}
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string parity = (string)value;

if (CultureInfo.CurrentCulture.Name == "en-US")
{
if (parity == "Odd")
{
return Parity.Odd;
}
else if (parity == "Even")
{
return Parity.Even;
}
else
{
return Parity.None;
}
}
else
{
if (parity == "奇")
{
return Parity.Odd;
}
else if (parity == "偶")
{
return Parity.Even;
}
else
{
return Parity.None;
}
}
}
}

public class AllParitiesToStringListConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
List<Parity> enums = value as List<Parity>;
List<string> result = new List<string>();

if (CultureInfo.CurrentCulture.Name == "en-US")
{
foreach (var item in enums)
{
switch (item)
{
case Parity.Odd:
result.Add("Odd");
break;
case Parity.Even:
result.Add("Even");
break;
case Parity.None:
result.Add("None");
break;
}
}
}
else
{
foreach (var item in enums)
{
switch (item)
{
case Parity.Odd:
result.Add("奇");
break;
case Parity.Even:
result.Add("偶");
break;
case Parity.None:
result.Add("无");
break;
}
}
}

return result;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("The method or operation is not implemented.");
}
}

 

最简单的WPF的ComboBox显示多语言的Enum

标签:

原文地址:http://www.cnblogs.com/cocochow/p/5789656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!