WPF Toggle Button Checked/Uchecked event with one handler
Asked Answered
B

3

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.

Beadroll answered 6/10, 2011 at 17:5 Comment(2)
Do you want single event for checked and unchecked event or you want two different handlers but at global level i.e. only in one file?Enlistee
Yea just for checked and unchecked. Thanks!Beadroll
E
29

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>
Enlistee answered 6/10, 2011 at 17:31 Comment(11)
I get the following error: Cannot convert type bool? to bool. How can I fix this safely?Beadroll
Null-coalescing operator works well for this. ex) if((sender as ToggleButton).IsChecked ?? false)Beadroll
Is your toggleButton is three state or two state?Enlistee
If it's two state in that case you can just do like this - if((sender as ToggleButton).IsChecked.Value) {}Enlistee
Thanks, yes it is 2 state. Checked/UncheckedBeadroll
@Ryan R There's one problem here. Checkbox changes its state on num +/- but Click event is not triggered.Featurelength
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.Nicolette
@Nicolette - Please read the question. OP was interested in only handling it from UI. So, in current context answer is perfectly fine.Enlistee
@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".Nicolette
@Nicolette - OP already accepted the answer so that should clear the intention of OP. Thanks anyways.. :)Enlistee
@Rohit Vats: Yeah, just wanted to clarify for new comers. Have a good day there :)Nicolette
N
22

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" />
Nicolette answered 10/2, 2016 at 16:2 Comment(0)
G
0

Try this

private void tBtn_super_Click(object sender, RoutedEventArgs e)
        {
            if (tBtn_super.IsChecked == true)
            {
                MessageBox.Show("True");
            }
            else
            {
                MessageBox.Show("False");
            }
        }
Grano answered 3/9, 2014 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.