1

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?

DeMama
  • 1,134
  • 3
  • 13
  • 32

3 Answers3

1

I can tell you right away that a style won't solve your problem because once a user sets the value of a DependencyProperty it ignores style setters, you should read up on Dependency Property Value Precedence.

On the other hand you can try handling the mouse click on your own when a radio button has IsChecked=True, like so:

        <RadioButton GroupName="AAA" PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"/>

And:

    private void RadioButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        RadioButton radioButton = sender as RadioButton;

        if (radioButton != null && radioButton.IsChecked.GetValueOrDefault())
        {
            radioButton.IsChecked = false;
            e.Handled = true;
        }
    }

I think this does the trick.

Yoav
  • 115
  • 1
  • 1
  • 7
0

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.

icebat
  • 4,696
  • 4
  • 22
  • 36
  • @DeMama, would you mind to share your solution (via edit or separate answer)? It might be really useful for others, and quite interesting for us, who answered the question =) – icebat Oct 31 '14 at 12:48
  • Here you go. However, please note that this solution is very specific for what I needed. – DeMama Oct 31 '14 at 13:10
0

As icebat requested, i'll post my solution:

I simply created an event handler for my RadioButtons where I compare certain values defined in my class:

private int PopupID;
private bool IsPopupActive;
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    string s = (string)rb.Content;
    int id = int.Parse(s);

    if (PopupID == id && IsPopupActive == true)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>())
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = false;
    }
    else if (PopupID != id || IsPopupActive == false)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>()
                .Where(x => x != rb))
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = true;
        PopupID = id;
    }
}

Not a big fan of code-behind solutions like this, but it works.

Community
  • 1
  • 1
DeMama
  • 1,134
  • 3
  • 13
  • 32