RadioButton Control For Windows 10

Radio Button represents a button that allows a user to select a single option from a group of options. We use RadioButton controls to limit a user's selection to a single choice within a set of related, but mutually exclusive, choices. You can group RadioButton controls by putting them inside the same parent container or by setting the GroupName property on each RadioButton to the same value.

In the following demo we are making two groups of radio button for Department and Year from which user can select and the selected will be shown in a TextBlock shown.

Step 1: Open a blank app and add two sets of RadioButton and a TextBlock for each either from the toolbox or by copying the following XAML code into your grid.

  1. <TextBlock Text="Radio Button" FontSize="20"></TextBlock>  
  2. <StackPanel Margin="10,30,0,0">  
  3.     <StackPanel Orientation="Horizontal">  
  4.         <TextBlock Margin="0,5,0,0" Text="Select your department"></TextBlock>  
  5.         <StackPanel>  
  6.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="CSE" Checked="deptRadioButton_Checked"></RadioButton>  
  7.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="ECE" Checked="deptRadioButton_Checked"></RadioButton>  
  8.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="EEE" Checked="deptRadioButton_Checked"></RadioButton>  
  9.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="Civil" Checked="deptRadioButton_Checked"></RadioButton>  
  10.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="Mech" Checked="deptRadioButton_Checked"></RadioButton>  
  11.             <RadioButton Margin="10,0,0,0" GroupName="dept" Content="IT" Checked="deptRadioButton_Checked"></RadioButton>  
  12.         </StackPanel>  
  13.         <TextBlock Margin="0,10,0,0" Name="selectedDept" Foreground="Green"></TextBlock>  
  14.     </StackPanel>  
  15.     <StackPanel Margin="0,20,0,0" Orientation="Horizontal">  
  16.         <TextBlock Margin="0,5,0,0" Text="Select your year"></TextBlock>  
  17.         <StackPanel>  
  18.             <RadioButton Margin="60,0,0,0" GroupName="year" Content="First" Checked="yearRadioButton_Checked"></RadioButton>  
  19.             <RadioButton Margin="60,0,0,0" GroupName="year" Content="Second" Checked="yearRadioButton_Checked"></RadioButton>  
  20.             <RadioButton Margin="60,0,0,0" GroupName="year" Content="Third" Checked="yearRadioButton_Checked"></RadioButton>  
  21.             <RadioButton Margin="60,0,0,0" GroupName="year" Content="Final" Checked="yearRadioButton_Checked"></RadioButton>  
  22.         </StackPanel>  
  23.         <TextBlock Margin="0,10,0,0" Name="selectedYear" Foreground="Green"></TextBlock>  
  24.     </StackPanel>  
  25. </StackPanel>   

A RadioButton has two states: selected or cleared. When a RadioButton is selected, its IsChecked property is true. When a RadioButton is cleared, its IsChecked property is false. A RadioButton can be cleared by clicking another RadioButton in the same group, but it cannot be cleared by clicking it again. However, a RadioButton can be cleared programmatically by setting its IsChecked property to false.


Step 2:  Add the following code to your cs page to handle the event handlers while being getting checked or cleared. 

  1. private void deptRadioButton_Checked(object sender, RoutedEventArgs e)    
  2. {    
  3.     RadioButton rbd = sender as RadioButton;    
  4.     selectedDept.Text ="You have selected " + rbd.Content.ToString();    
  5. }    
  7. private void yearRadioButton_Checked(object sender, RoutedEventArgs e)    
  8. {    
  9.     RadioButton rby = sender as RadioButton;    
  10.     selectedYear.Text = "You have selected " + rby.Content.ToString();    
  11. }   
Step 3: Run your application and check yourself.

Up Next
    Ebook Download
    View all
    View all