How to use the radiobutton in WPF and XAML.

How to use the radiobutton in WPF and XAML.

Title: How to use the radiobutton in WPF and XAML.

 

WPF RadioButton Control:

 

RadioButton controls are usually grouped together to offer user a single choice among several options (only one button at a time can be selected).

 

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

 

Properties:

In the following tag  I create the RadioButton in which Height is height of the RadioButton, Name is the nameof the RadioButton, text in the between the RadioButton tag is the centent visibile to user. Background the the color of the box, Borderbrush is the color of the border, Forground is the color of the text.

 

<RadioButton Height="16" Margin="26,18,132,0" Name="RadioButton_Option1" VerticalAlignment="Top" Background="Snow" BorderBrush="Black"  GroupName="Visit_eggHeadcafe.com" Foreground="DarkBlue">ASP.net Articles </RadioButton> 

 

 

To change the orientation from left to right use the FlowDirection property to RightToLeft.

 

 RadioButton is used in the group so that user can select only one option from the available options (No extra coding is required to uncheck others). Use same GroupName of the radiobuttons to mark in a group so that only one option can be selected as follows.

 

<StackPanel >

        <RadioButton Height="16" Margin="26,18,132,0" Name="RadioButton_Option1" VerticalAlignment="Top" Background="Snow" BorderBrush="Black"  GroupName="Visit_eggHeadcafe.com" Foreground="DarkBlue">ASP.net Articles </RadioButton>

        <RadioButton Height="16" Margin="26,18,132,0" Name="RadioButton_Option2" VerticalAlignment="Top" Background="Snow" BorderBrush="Black"  GroupName="Visit_eggHeadcafe.com" Foreground="DarkBlue">C# Articles</RadioButton>

        <RadioButton Height="16" Margin="26,18,132,0" Name="RadioButton_Option3" VerticalAlignment="Top" Background="Snow" BorderBrush="Black"  GroupName="Visit_eggHeadcafe.com" Foreground="DarkBlue">ADO.net Articles</RadioButton>

        <RadioButton Height="17" Margin="26,18,115,0" Name="RadioButton_Option4" VerticalAlignment="Top" Background="Snow" BorderBrush="Black"  GroupName="Visit_eggHeadcafe.com" Foreground="DarkBlue" Width="164">SQL Server 2005 Articles</RadioButton>

        <Button  Margin="26,18,132,0" Width="75" Height="20" Click="Button_Click">Open Articles</Button>

        </StackPanel > 

Events:

 

 

Now write the code to the handler so that we can take appropriate action on these events in windows1.xaml.cs.

 

 

Finding out which radiobutton is checked use the property IsChecked as follows in the btnShow  button

 

        private void btnShow_Click(object sender, RoutedEventArgs e)

        {

            if (RadioButton_Option1.IsChecked==true)

 

                System.Diagnostics.Process.Start(@"http://www.eggheadcafe.com/ASPNetBlog.aspx");

                else if(RadioButton_Option2.IsChecked==true)

                System.Diagnostics.Process.Start(@"http://www.eggheadcafe.com/CSharpBlog.aspx");

                else if(RadioButton_Option3.IsChecked==true)

                System.Diagnostics.Process.Start(@"http://www.eggheadcafe.com/ADONetBlog.aspx");

                else if(RadioButton_Option4.IsChecked==true)

                System.Diagnostics.Process.Start(@"http://www.eggheadcafe.com/SQLServerBlog.aspx");

 

 

        } 

 

Summary: Radio button is very simple control and used for selecting only one option from the available options.

 

 

 

 

By Kalit Sikka   Popularity  (7755 Views)