Search code examples
wpfdata-bindingradio-button

WPF RadioButton Databinding with no selection


I've followed the example project at https://www.codeproject.com/Tips/720497/Binding-Radio-Buttons-to-a-Single-Property?fid=1853040&df=90&mpp=25&sort=Position&spc=Relaxed&view=Normal&prof=True&tid=5018079 and I can successfully databind multiple radio buttons so a single backend value for the group. So far I've just gone with the Yes / No option onto a bool and the code as is produces this

enter image description here

I wanted to maintain a default of both not being checked for a brand new data entry form to force the user to make a selection and figured that simply making the vm binding property type bool? but what I end up with is both buttons selected and the colour gone grey looking like they're disabled, although I can click one and make a selection.

enter image description here

Any idea how to get them to load as unselected as when I don't databind?

Here's the guts of what's making this work

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

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
    }
}


<Window.Resources>
    <ObjectDataProvider x:Key="FindCriteria" ObjectType="{x:Type src:TestOptions}" />
    <src:RadioButtonCheckedConverter x:Key="RadioButtonCheckedConverter" />
</Window.Resources>
.
.
.
<RadioButton GroupName="BooleanGroup" Grid.Row="1" Margin="0,5" 
                IsChecked="{Binding BooleanProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
                ConverterParameter={x:Static src:MainWindow.BooleanTrue}}">
    <TextBlock TextWrapping="Wrap" Text="True" />
</RadioButton>

<RadioButton GroupName="BooleanGroup" Grid.Row="2" Margin="0,5" 
                IsChecked="{Binding BooleanProperty, Converter={StaticResource RadioButtonCheckedConverter}, 
                ConverterParameter={x:Static src:MainWindow.BooleanFalse}}">
    <TextBlock TextWrapping="Wrap" Text="False"/>
</RadioButton>


public MainWindow()
{
    InitializeComponent();

    this.options = new TestOptions();
    //this.options.BooleanProperty = true;
    this.DataContext = this.options;
}  

public class TestOptions
{
    public bool? BooleanProperty { get; set; }
}

Solution

  • Ahah!! Got tinkering and found that setting the bound calls from all buttons in the group to false when they're null in the value converter seems

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var ret = value?.Equals(parameter) ?? false;
        return ret;
    }
    

    Now on load:

    enter image description here

    and selecting options:

    enter image description here

    enter image description here