Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple converter parameter in generic Enum to Boolean Converter

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>
like image 997
Yogesh Avatar asked Dec 12 '22 11:12

Yogesh


1 Answers

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.

like image 84
Nikita B Avatar answered May 09 '23 20:05

Nikita B