WPF Radiobutton (two) (binding to boolean value) WPF Radiobutton (two) (binding to boolean value) wpf wpf

WPF Radiobutton (two) (binding to boolean value)


<RadioButton GroupName="Group1"              IsChecked="{Binding PropertyValue}" Content="Yes" /><RadioButton GroupName="Group1"  Content="No"              IsChecked="{Binding PropertyValue,                          Converter={StaticResource BoolInverterConverter}}" />
public class BoolInverterConverter : IValueConverter{    #region IValueConverter Members    public object Convert(object value, Type targetType, object parameter,        System.Globalization.CultureInfo culture)    {        if (value is bool)        {            return !(bool)value;        }        return value;    }    public object ConvertBack(object value, Type targetType, object parameter,         System.Globalization.CultureInfo culture)    {        if (value is bool)        {            return !(bool)value;        }        return value;    }    #endregion}


The standard binding approach has the unfortunate side effect of firing the binding setter as "unselected" whenever the UI is loaded. So if you've got code to handle the user's clicks in the setter for your bool, it will do some weird stuff like fire the setter to "false" even though you've bound it to a "true" bool.

I got around this with a converter used specifically for radio buttons:

public class BoolRadioConverter : IValueConverter{    public bool Inverse { get; set; }    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    {        bool boolValue = (bool) value;        return this.Inverse ? !boolValue : boolValue;    }    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    {        bool boolValue = (bool)value;        if (!boolValue)        {            // We only care when the user clicks a radio button to select it.            return null;        }        return !this.Inverse;    }}

In your resources:

<converters:BoolRadioConverter x:Key="BoolRadioConverter" /><converters:BoolRadioConverter x:Key="InverseBoolRadioConverter" Inverse="True" />

In your xaml:

<RadioButton    Content="True option"    GroupName="radioGroup1"    IsChecked="{Binding MyProperty,                        Converter={StaticResource BoolRadioConverter}}" /><RadioButton    Content="False option"    GroupName="radioGroup2"    IsChecked="{Binding MyProperty,                        Converter={StaticResource InverseBoolRadioConverter}}" />


You can use a value-converter that reverts the boolean value:

With that converter, bind one Checkbox.IsChecked-property to the boolean value without the converter and one CheckBox.IsChecked-property with the converter. This should do the trick.

Here the code for such a converter. I have copied it from here and added some lines of code. There you will find more information about.

public class BoolToOppositeBoolConverter : IValueConverter {    public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture) {        if (targetType != typeof(bool)) {            throw new InvalidOperationException("The target must be a boolean");        }        if (null == value) {            return null;        }                    return !(bool)value;    }    public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture) {        if (targetType != typeof(bool)) {            throw new InvalidOperationException("The target must be a boolean");        }        if (null == value) {            return null;        }        return !(bool)value;    }} 

To use it, declare it in the resource-section.

 <local:BoolToOppositeBoolConverter x:Key="BoolToOppositeBoolConverter_ValueConverter"/>

And the use it in the binding as a static resource:

<CheckBox IsChecked="{Binding YourProperty}" /><CheckBox IsChecked="{Binding YourProperty,Converter={StaticResource BoolToOppositeBoolConverter_ValueConverter}}" />

Please note, the converter is only a simple example. Implement it neatly if you want to use it in productive code. I have not tested it. Make a comment if its not working.