Search code examples
c#wpfradio-buttonischecked

How to set all RadioButton IsChecked properties to false


I have 4 RadioButtons in a UniformGrid.

Initially they are all set to false.

But once I click one of them, one stays true at all times.

Our requirement is that they all can be set to false.

I tried things like

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding IsChecked,
                   RelativeSource={RelativeSource Self}}" Value="True"/>
        <Condition Binding="{Binding IsPressed,
                   RelativeSource={RelativeSource Self}}" Value="True"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="IsChecked" Value="False"/>
</MultiDataTrigger>

But it's not working. I know I can use a ToggleButton, but only one of my RadioButtons can be checked at a time.

Is this even possible?


Solution

  • Your solution works only when mouse button is actually pressed (try to hold it and you'll see). The easiest way that comes to mind is to alter RadioButton a bit to suit our needs:

    public class UncheckableRadioButton : RadioButton
    {
       protected override void OnToggle()
       {
          if (IsChecked == true)
             IsChecked = false;
          else base.OnToggle();
       }
    }
    

    Theese custom radios will uncheck if clicked again.