I have been through this How to bind RadioButtons to an enum?
and accepted answer to this question contains use of generic Enum to Boolean converter.
my problem is that I am having two radio buttons in View and an enum
public Enum LinkType
{
A,
B,
C,
D,
E,
F
}
In ViewModel I have a property Called
public LinkType MyLinktype
{
get;set;
}
my first radio button can be true if property of enum in ViewModel is having value among A,C,E and second radio button can be true if property of enum in ViewModel is having value among. B,D,F
So, How can I pass multiple values in the converter parameter in generic EnumTo Boolean Converter which is as following
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);
}
So what changes I have to make in converter if I want something like this in the 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>
You can define an array in xaml:
<x:Array Type="LinkType" x:Key="ar">
<LinkType>A</LinkType>
<LinkType>B</LinkType>
</x:Array>
And then pass it as parameter
<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter={StaticResource ar}}">Odd LinkType</RadioButton>
You'll have to fix your converter tho, in order to properly handle array as converter parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With