Skip to main content

UWP - How to use RadioButton

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.
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;
                }
            }
        }
    }
}

Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...