UWP RadioButton
The RadioButton class represents a button that allows UWP app users to
select a single option from a group of options. The UWP app developers use
RadioButton controls to limit the app user’s selection to a single choice
within a set of related, but mutually exclusive, choices. The developers can
group RadioButton controls by putting them inside the same parent container or
by setting the GroupName property on each RadioButton to the same value.
The RadioButton control has two states, they are selected or cleared. The RadioButton’s IsChecked property value true indicates the RadioButton is selected and the value false indicates the RadioButton selection is cleared.
The UWP app user can clear a RadioButton selection by clicking another RadioButton in the same group. But they cannot clear the RadioButton by clicking it again. The UWP developers can clear a RadioButton programmatically by setting its IsChecked property value to false.
The RadioButton class’s GroupName property gets or sets the name that specifies which RadioButton controls are mutually exclusive. This property value is a String which is the name that specifies which RadioButton controls are mutually exclusive. The default value of this property is null.
The FrameworkElement class’s Tag property gets or sets an arbitrary object value that can be used to store custom information about this object. This property value is an Object instance which is the intended arbitrary object value. The Tag property has no default value.
The ToggleButton class’s Checked event fires when a ToggleButton is checked. The following UWP app development tutorial code will demonstrate how we can use RadioButton control. Here we group RadioButton controls to let users select an option.
The RadioButton control has two states, they are selected or cleared. The RadioButton’s IsChecked property value true indicates the RadioButton is selected and the value false indicates the RadioButton selection is cleared.
The UWP app user can clear a RadioButton selection by clicking another RadioButton in the same group. But they cannot clear the RadioButton by clicking it again. The UWP developers can clear a RadioButton programmatically by setting its IsChecked property value to false.
The RadioButton class’s GroupName property gets or sets the name that specifies which RadioButton controls are mutually exclusive. This property value is a String which is the name that specifies which RadioButton controls are mutually exclusive. The default value of this property is null.
The FrameworkElement class’s Tag property gets or sets an arbitrary object value that can be used to store custom information about this object. This property value is an Object instance which is the intended arbitrary object value. The Tag property has no default value.
The ToggleButton class’s Checked event fires when a ToggleButton is checked. The following UWP app development tutorial code will demonstrate how we can use RadioButton control. Here we group RadioButton controls to let users select an option.
MainPage.xaml
<Page
x:Class="UniversalAppTutorials.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalAppTutorials"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel
x:Name="stack_panel1"
Margin="50"
Orientation="Vertical"
Background="AliceBlue"
Padding="35"
>
<TextBlock
x:Name="text_block1"
TextWrapping="WrapWholeWords"
Text="Change This text color by choosing a color from radio btton group."
Margin="0,0,0,10"
FontSize="30"
FontFamily="s"
/>
<TextBlock
Text="Choose a Color."
Margin="10"
FontWeight="Black"
/>
<StackPanel
Orientation="Horizontal"
Background="Orange"
Padding="20"
>
<RadioButton
Content="Red"
Tag="Red"
Checked="Color_RadioButton_Checked"
GroupName="TextColor"
/>
<RadioButton
Content="Green"
Tag="Green"
Checked="Color_RadioButton_Checked"
GroupName="TextColor"
/>
<RadioButton
Content="Blue"
Tag="Blue"
Checked="Color_RadioButton_Checked"
GroupName="TextColor"
/>
<RadioButton
Content="Yellow"
Tag="Yellow"
Checked="Color_RadioButton_Checked"
GroupName="TextColor"
/>
</StackPanel>
</StackPanel>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI;
namespace UniversalAppTutorials
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Color_RadioButton_Checked(object sender, RoutedEventArgs e) {
// Get the instance of clicked RadioButton instance
RadioButton rb = (RadioButton)sender;
if (rb != null && text_block1 != null) {
string selectedColor = rb.Tag.ToString();
switch (selectedColor) {
case "Red":
text_block1.Foreground = new SolidColorBrush(Colors.Red);
break;
case "Green":
text_block1.Foreground = new SolidColorBrush(Colors.Green);
break;
case "Blue":
text_block1.Foreground = new SolidColorBrush(Colors.Blue);
break;
case "Yellow":
text_block1.Foreground = new SolidColorBrush(Colors.Yellow);
break;
}
}
}
}
}


