26

I am using a ToggleButton in a WPF window:

 <ToggleButton Height="37"
          HorizontalAlignment="Left"
          Margin="485.738,254.419,0,0"
          VerticalAlignment="Top"
          Width="109"
          IsEnabled="True"
          Checked="toggleAPDTimeoutErr_Checked"
          Unchecked="toggleAPDTimeoutErr_Unchecked">Timeout</ToggleButton>

I have two events that I am monitoring, but this is done in two different code behind handlers. How can this be done in only one?

I will have many ToggleButtons, and the code can get large.

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
Ryan R
  • 8,342
  • 15
  • 84
  • 111

3 Answers3

30

You can attach a single click event of your ToggleButton and in its handler you can check the ToggleButton IsChecked property by type casting the sender object in your handler like this -

private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
   if((sender as ToggleButton).IsChecked)
   {
      // Code for Checked state
   }
   else
   {
      // Code for Un-Checked state
   }
}

Xaml:

<ToggleButton Height="37" HorizontalAlignment="Left" Margin="485.738,254.419,0,0"     VerticalAlignment="Top" Width="109" IsEnabled="True" Click="ToggleButton_Click">Timeout</ToggleButton>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • I get the following error: Cannot convert type bool? to bool. How can I fix this safely? – Ryan R Oct 06 '11 at 18:26
  • 5
    Null-coalescing operator works well for this. ex) `if((sender as ToggleButton).IsChecked ?? false)` – Ryan R Oct 06 '11 at 18:30
  • Is your toggleButton is three state or two state? – Rohit Vats Oct 06 '11 at 18:33
  • 7
    If it's two state in that case you can just do like this - if((sender as ToggleButton).IsChecked.Value) {} – Rohit Vats Oct 06 '11 at 18:35
  • Thanks, yes it is 2 state. Checked/Unchecked – Ryan R Oct 06 '11 at 21:59
  • @Ryan R There's one problem here. Checkbox changes its state on num +/- but Click event is not triggered. – Pavel Voronin Feb 24 '12 at 14:42
  • I down voted as this solution is a bug. Changing IsChecked from code (`toggleButton.IsChecked = false`) or by other input devices will not fire the event. – Arek Feb 10 '16 at 15:57
  • @Arek - Please read the question. OP was interested in only handling it from UI. So, in current context answer is perfectly fine. – Rohit Vats Feb 11 '16 at 08:06
  • @Rohit Vats - what does "handling it from UI" mean? Please read original question. OP did not write "my users are only using mouse, keyboard input is not needed at all, and I'm not going to use animations or anything else to switch this property". – Arek Feb 12 '16 at 08:28
  • @Arek - OP already accepted the answer so that should clear the intention of OP. Thanks anyways.. :) – Rohit Vats Feb 15 '16 at 05:58
  • @Rohit Vats: Yeah, just wanted to clarify for new comers. Have a good day there :) – Arek Feb 15 '16 at 07:57
21

You should not use Click event as some answers suggest, because it will not work when the property IsChecked is changed by code or any other event than mouse (keyboard, animation..). This is simply a bug.

Instead you can use the same handler for both Checked and Unchecked and do action depending on IsChecked property.

<ToggleButton
    Checked="toggleButton_IsCheckedChanged"
    Unchecked="toggleButton_IsCheckedChanged" />
Arek
  • 1,276
  • 1
  • 10
  • 19
0

Try this

private void tBtn_super_Click(object sender, RoutedEventArgs e)
        {
            if (tBtn_super.IsChecked == true)
            {
                MessageBox.Show("True");
            }
            else
            {
                MessageBox.Show("False");
            }
        }