
Namespace: System.Windows.Controls
Library: PresentationFramework
The RadioButton is a control that is usually used as an item in a group however, it is possible to create a single radio button. Unlike a CheckBox, the user cannot clear the selected RadioButton by clicking it again.
We modify a little the previous demo. Look the code:
The Window1.xaml file is:
<Window
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
x:Class="DMU.Window1"
Title="DMU Tutorials Serie"
Top="100"
Left= "100"
Width="400"
Height="400"
>
<Canvas Width="400" Height="400" Background="DeepSkyBlue" >
<TextBlock Name="Text1" Canvas.Top="50" Canvas.Left="100" FontSize="14">Choose one of the pieces:</TextBlock>
<RadioButton GroupName="grp" Name="Bt1" Canvas.Top="100" Canvas.Left="100" Click="Hat">Hat</RadioButton>
<RadioButton GroupName="grp" Name="Bt2" Canvas.Top="100" Canvas.Left="200" Click = "Shoe">Shoe</RadioButton>
<RadioButton GroupName="grp" Name="Bt3" Canvas.Top="100" Canvas.Left="300" Click = "Shirt">Shirt</RadioButton>
<TextBlock Canvas.Top="200" Canvas.Left="100" FontSize="14">Your selection was:</TextBlock>
<TextBox Name = "TheItem" Canvas.Top="300" Canvas.Left="100" FontSize="14"/>
</Canvas>
</Window>
And the Window1.xaml.cs file is:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace DMU{
public partial class Window1 : Window{
public Window1(){
InitializeComponent();
}
private void Hat(object sender, RoutedEventArgs e){
TheItem.Clear();
TheItem.Text = "Hat" ;
}
private void Shoe(object sender, RoutedEventArgs e){
TheItem.Clear();
TheItem.Text = "Shoe";
}
private void Shirt(object sender, RoutedEventArgs e){
TheItem.Clear();
TheItem.Text = "Shirt";
}
}
}
IMPORTANT: We are not presenting the other 3 usual files of the application.
You can run the application. Please, you need to liberate the firewall.
![]() |
After this introduction, some properties:
Background
The value will be a color.
BorderBrush
Also a color.
BorderThickness
Click
GroupName
Height
IsChecked
You need to use True | False. It's a read/write property.
IsEnabled
Margin
Name
Opacity
Visibility
Width
Some events. To be overrided.
The more important (you can see how to use it at the sample):
Click
Unchecked
Captures when the RadioButton is not anymore clicked.
KeyDown
KeyUp
And the usual mouse events:
MouseDoubleClick MouseDown MouseEnter MouseLeave MouseLeftButtonDown MouseLeftButtonUp MouseMove MouseRightButtonDown MouseRightButtonUp MouseUp
OK. At this 24 lessons we tried to present the basics of the programming using WPF and C# for web applications. Now, you can read our tutorials about 3D game creation going to our homepage. Having doubts, read the SDK Documentation and try to contact the official WPF Forum at this link.
Thanks.
PREVIOUS LESSON T.CONTENTS HOMEPAGE
