0

I have a scenario where I have kept three different properties for a group of radio buttons Is it possible to make it single?

<RadioButton GroupName="os" IsChecked="{Binding Age} IsChecked="{Binding Name,Mode=TwoWay,Converter={StaticResource enumConverter},ConverterParameter=a}"/>
<RadioButton GroupName="os" IsChecked="{Binding Age,Mode=TwoWay,Converter={StaticResource enumConverter},ConverterParameter=b}"/>
<RadioButton GroupName="os" IsChecked="{Binding Age,Mode=TwoWay,Converter={StaticResource enumConverter},ConverterParameter=c}"/>

Here I Need to write a StringConverter, where I will passing the converter parameter as A,B,C for respective radiobuttons.

 public class EnumMatchToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
            return false;

        //string checkValue = value.ToString();
        //string targetValue = parameter.ToString();
        //return checkValue.Equals(targetValue,
        //         StringComparison.InvariantCultureIgnoreCase);
        if (value.ToString() == "True" && (parameter.ToString() == "a" || parameter.ToString() == "b" || parameter.ToString() == "c"))
        {
            return true;
        }
        else
            return false;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
        return null;

        //bool useValue = (bool)value;
        //string targetValue = parameter.ToString();
        //if (useValue)
        //    return Enum.Parse(targetType, targetValue);

        //return null; 

        if (value.ToString() == "True" && (parameter.ToString() == "b" || parameter.ToString() == "a" || parameter.ToString() == "c"))
        {
            return true;
        }
        else
            return false;

    }
}
Sundar Stalin
  • 99
  • 2
  • 2
  • 9

1 Answers1

0

Try this:

public class EnumMatchToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
            return false;

        return value.ToString() == parameter.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isChecked = (bool)value;
        if (isChecked)
            return parameter.ToString();

        return Binding.DoNothing;

    }
}

You may also want to consider renaming the converter class to something more appropriate.

mm8
  • 163,881
  • 10
  • 57
  • 88