CheckBox represents a control that a user can select (check) or clear (uncheck) form a list of options that a user can select, such as a list of settings to apply to an application. Both CheckBox and RadioButton controls allow the user to select from a list of options. CheckBox controls allow the user to select a combination of options. In contrast, RadioButton controls allow the user to select from mutually exclusive options.

In the following demo we are listing some skills from which user can select and the selected will be shown in a TextBlock shown below and unchecking the same will remove it also.

Step 1: Open a blank app and add some CheckBox and a Textblock control either from the toolbox or by copying the following XAML code into your grid.
  1. <StackPanel>  
  2.     <TextBlock Text="Check Box" FontSize="20"></TextBlock>  
  3.     <StackPanel Margin="10,10,0,0" Orientation="Horizontal">  
  4.         <TextBlock Text="Select your skills"></TextBlock>  
  5.         <StackPanel>  
  6.             <CheckBox Margin="20,0,0,0" Content="C#" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>  
  7.             <CheckBox Margin="20,0,0,0" Content="XAML" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>  
  8.             <CheckBox Margin="20,0,0,0" Content="HTML" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>  
  9.             <CheckBox Margin="20,0,0,0" Content="JAVA" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>  
  10.             <CheckBox Margin="20,0,0,0" Content="SQL" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>  
  11.         </StackPanel>  
  12.     </StackPanel>  
  13.     <TextBlock Name="selectedSkills" Margin="20,10,0,0" Foreground="Green"></TextBlock>  
  14. </StackPanel>  
All check boxes and navigating to the same event handler while getting checked or unchecked. 

 

Step 2: Add the following code to your cs page to handle the event handlers while being getting checked or unchecked.  
  1. List<string> selected = new List<string>();    
  2.     
  3. private void CheckBox_Checked(object sender, RoutedEventArgs e)    
  4. {               
  5.     CheckBox cb = sender as CheckBox;    
  6.     selected.Add(cb.Content.ToString());    
  7.     selectedSkills.Text = "Your skills are " + String.Join(", ", selected);                        
  8.        
  9. }    
  10.     
  11. private void CheckBox_Unchecked(object sender, RoutedEventArgs e)    
  12. {    
  13.     CheckBox cb = sender as CheckBox;    
  14.     selected.Remove(cb.Content.ToString());    
  15.     selectedSkills.Text = "Your skills are " + String.Join(", ", selected);    
  16. }   
Here we have created a list of strings to which the content value of each CheckBox get added or removed while getting checked and unchecked respectively.  

Step 3: Run your application and check yourself. 
 
 

Next Recommended Readings