Checked Event Handling of RadioButton Controls WPF in VB.NET

In this article I am going to explain how to add checked event handler in RadioButton in WPF.
  • 3151

RadioButton controls are usually grouped together to offer user a single choice among several options by selecting only one button at a time. There is no need for additional coding uncheck others. Use same GroupName of the radiobuttons to mark in a group so that only one option can be selected.

<RadioButton> </Radiobutton>  tag is used to create the radio button in XAML.

 

Adding a Checked Event Handler 

RadioButton control has Checked event as default event and raised when you check a radio button. The following code snippet adds the event handler.  

<Window x:Class="WpfApplication1.Window1" 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" SizeToContent="Height" Width="300">
    <Grid Name="grid">
        <Grid.RowDefinitions>  
       
<RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" />
        <Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" />
        <StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
            <TextBlock FontSize="16" Text="Gender" />
            <RadioButton Content="Male" GroupName="Group1"  Margin="5" Name="rbnOneA" />
            <RadioButton Content="Female" GroupName="Group1" Margin="5" Name="rbnOneB" />
            <Button Content="Show the Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center"
                Margin="10" MaxHeight="25" Click="Button_Click" />
            <Separator/>
            <TextBlock FontSize="16" Text="Marital Status" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Married" 
                Margin="5" Name="rbnTwoA" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Unmarried" Margin="5"  Name="rbName="rbnTwoB"/>
        <Button Content="Show the Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center"
                Margin="10" MaxHeight="25" Click="Button_Click" />
        </StackPanel>
                <StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
            <TextBlock FontSize="16" Text="Membership" />
            <RadioButton Content="Permanent Member" GroupName="Group3" Margin="5" Name="rbnThreeA" />
            <RadioButton Content="New User" GroupName="Group3" Margin="5" Name="rbnThreeB" />
       <Button Content="Show the Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center"
                Margin="10" MaxHeight="25" Click="Button_Click" />
                </StackPanel>       
   
</Grid>
</
Window>

OUTPUT

radiobutton.gif
 

CONCLUSION

I hope this article will help you understand RadioButton Controls in WPF.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.