Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Toggle Button Checked/Uchecked event with one handler

Tags:

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.

like image 310
Ryan R Avatar asked Oct 06 '11 17:10

Ryan R


1 Answers

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> 
like image 179
Rohit Vats Avatar answered Oct 01 '22 20:10

Rohit Vats