标签:
<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.");
}
}
标签:
原文地址:http://www.cnblogs.com/cocochow/p/5789656.html