Home > Back-end >  Selecting an already selected RadioButton will select all RadioButtons
Selecting an already selected RadioButton will select all RadioButtons

Time:02-02

I have two radio buttons and are binding them to an enum Like in enter image description here

Why and how fix it?

The whole project is accessible (hopefully, I have never done this before) at

https://github.com/Andis59/RadioButtons

CodePudding user response:

Your problem is the Converter. You are using a normal Enum but the converter treat it as [Flags] (which are a kind of different).

Enum.HasFlag(flag) will always return true if the ordinal value of the given flag is 0. Your WorkModeEnum.Auto has that ordinal value of 0 and causing this.

You could fix this by changing the WorkModeEnum definition to

public enum WorkModeEnum
{
    Auto = 1,
    Manual = 2,
}

but it is much better to fix the Converter

public class EnumToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((Enum)parameter).Equals((Enum)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}
  •  Tags:  
  • Related