Whenever you want to checked only one value in available items then we can use RadioButton control. RadioButton control has all the property like checkbox control except that in checkbox we can check multiple items while in radiobutton we can check only one item. In this demonstration I will tell you how to use radiobutton control in WPF.
Some common properties related with radio button radiobutton control
- Name—Name property is used to provide a unique name to your radiobutton control.
- GroupName—GroupName property is used to create a group for your radio button control. Basically group means among those values in which you want to select only one value.
- Content—Content property is used to provide some content to radiobutton control.
- FlowDirection—FlowDirection property used to set direction of content on control. It has two possible values one is LeftToRight and another is RightToLeft.
- IsChecked—IsChecked property is used to check whether radiobutton is checked or not.
- Checked—Basically checked is an event which fired whenever user checked the radiobutton control.
- Unchecked—Unchecked is another event in radiobutton which is fired whenever a radiobutton is unchecked.
Code snippet for creating radiobutton control
<RadioButton Content="RadioButton1" Height="20" HorizontalAlignment="Left" Margin="121,92,0,0" x:Name="rr1" VerticalAlignment="Top" GroupName="SampleGroup" Checked="rr1_Checked" />
<RadioButton Content="RadioButton2" Height="20" HorizontalAlignment="Left" Margin="257,92,0,0" x:Name="rr2" VerticalAlignment="Top" GroupName="SampleGroup" Checked="rr2_Checked" />
<Label Height="28" HorizontalAlignment="Left" Margin="96,130,0,0" x:Name="lblMessage" VerticalAlignment="Top" Width="284" />
//This event is fired when user checked first radio button.
private void rr1_Checked(object sender, RoutedEventArgs e)
lblMessage.Content = "Radio button 1 is checked.";
//This event is fired when user checked second radio button
private void rr2_Checked(object sender, RoutedEventArgs e)
lblMessage.Content = "Radio button 2 is checked.";
Output of the following code snippet is as follows
Whenever any user checked any radiobutton then message is displayed on label that radiobutton 1 is checked or radiobutton 2 is checked.