ARTICLE

Checked Event Handling of RadioButton Controls in WPF

Posted by Sapna Articles | WPF using VB.NET October 04, 2010
In this article I am going to explain how to add checked event handler in RadioButton in WPF.
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor