我经历过这个How to bind RadioButtons to an enum?
这个问题的公认答案包含通用枚举到 bool 转换器的使用。
我的问题是我在 View 中有两个单选按钮和一个枚举
public Enum LinkType
{
A,
B,
C,
D,
E,
F
}
在 ViewModel 中我有一个属性 Called
public LinkType MyLinktype
{
get;set;
}
如果 ViewModel 中的枚举属性在 A、C、E 之间具有值,我的第一个单选按钮可以为真,如果 ViewModel 中的枚举属性在其中具有值,则第二个单选按钮可以为真。 B,D,F
那么,如何在通用 EnumTo bool 转换器的转换器参数中传递多个值,如下所示
public class EnumBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
if (Enum.IsDefined(value.GetType(), value) == false)
return DependencyProperty.UnsetValue;
object parameterValue = Enum.Parse(value.GetType(), parameterString);
return parameterValue.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
return Enum.Parse(targetType, parameterString);
}
如果我想在 XAML 中使用类似的东西,我必须在转换器中做哪些更改
<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter=A,C,F}">Odd LinkType</RadioButton>
<RadioButton IsChecked="{Binding Path=Mylinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter=B,D,E}">Even Link Type</RadioButton>
最佳答案
你可以在xaml中定义一个数组:
<x:Array Type="LinkType" x:Key="ar">
<LinkType>A</LinkType>
<LinkType>B</LinkType>
</x:Array>
然后将其作为参数传递
<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter={StaticResource ar}}">Odd LinkType</RadioButton>
您必须修复您的转换器,以便正确处理数组作为转换器参数。
关于c# - 如何将通用枚举中的多个转换器参数传递给 bool 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18864627/