Toggle Button Control For Windows 10

Toggle Button act as a base class for controls that can switch states, such as CheckBox and RadioButton. It is a button that is mainly used for switching states. It is possible to have a three state also. It will check the IsChecked property to verify selected or not.

Step 1: Open a blank app and add a ToggleButton control either from the toolbox or by copying the following XAML code into your grid.

  1. <TextBlock Text="Toggle Button" FontSize="20"></TextBlock>  
  2. <StackPanel Margin="10,40,0,0">  
  3.     <StackPanel Orientation="Horizontal">  
  4.         <TextBlock Text="Toggle Button" VerticalAlignment="Center" FontSize="20"></TextBlock>  
  5.         <ToggleButton Name="myToggleButton" Content="OFF" Margin="15,0,0,0" Width="150" Height="50" Click="myToggleButton_Click">                </ToggleButton>  
  6.     </StackPanel>  
  7. </StackPanel>  

 

Step 2: Copy and paste the following code to the cs page to handle the events generated on checking and unchecking. 

  1. private void myToggleButton_Click(object sender, RoutedEventArgs e)  
  2. {   
  3.    if(myToggleButton.IsChecked == true)  
  4.     {  
  5.         myToggleButton.Content = "ON";  
  6.     }  
  7.     else  
  8.     {  
  9.         myToggleButton.Content = "OFF";  
  10.     }  
  11.  }  
Here we are changing the content of the button accordingly on clicking.
 
Step 3: Run your application and test yourself. 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all