Getting Started with .NET MAUI RadioButton
13 Nov 20246 minutes to read
This section guides you through setting up and configuring a RadioButton in your .NET MAUI application. Follow the steps below to add a basic RadioButton to your project.
To quickly get started with the .NET MAUI RadioButton, watch this video.
Prerequisites
Before proceeding, ensure the following are set up:
- Install .NET 8 SDK or later is installed.
- Set up a .NET MAUI environment with Visual Studio 2022 (v17.3 or later) or Visual Studio Code. For Visual Studio Code users, ensure that the .NET MAUI workload is installed and configured as described here.
Step 1: Create a New MAUI Project
Visual Studio
- Go to File > New > Project and choose the .NET MAUI App template.
- Name the project and choose a location. Then, click Next.
- Select the .NET framework version and click Create.
Visual Studio Code
- Open the command palette by pressing
Ctrl+Shift+P
and type .NET:New Project and enter. - Choose the .NET MAUI App template.
- Select the project location, type the project name and press Enter.
- Then choose Create project.
Step 2: Install the Syncfusion MAUI Buttons NuGet Package
- In Solution Explorer, right-click the project and choose Manage NuGet Packages.
- Search for Syncfusion.Maui.Buttons and install the latest version.
- Ensure the necessary dependencies are installed correctly, and the project is restored.
Step 3: Register the handler
Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace RadioButtonGettingStarted
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}
Step 4: Add a Basic RadioButton
- To initialize the control, import the Buttons namespace into your code.
- Initialize SfRadioButton.
<ContentPage
. . .
xmlns:buttons="clr-namespace:Syncfusion.Maui.Buttons;assembly=Syncfusion.Maui.Buttons">
<buttons:SfRadioButton x:Name="radioButton"/>
</ContentPage>
using Syncfusion.Maui.Buttons;
namespace RadioButtonGettingStarted
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
SfRadioButton radioButton = new SfRadioButton();
this.Content=radioButton
}
}
}
Step 5: Setting caption
The .NET MAUI Radio Button caption can be defined using the Text property. This caption typically describes the meaning of the radio button and is displayed next to the radio button.
<buttons:SfRadioButton x:Name="radioButton" Text="Radio Button"/>
SfRadioButton radioButton = new SfRadioButton();
radioButton.Text = "Radio Button";
this.Content = radioButton;
Change the Radio Button state
The two different visual states of the .NET MAUI Radio Button are:
- Checked
- Unchecked
To change the state of the .NET MAUI Radio Button, you can utilize the IsChecked property of SfRadioButton. When the Radio Button is checked, an inner circle is added to its visualization.
You can group multiple radio buttons together by using RadioGroup. Only one button within a group can be selected at a time.
<buttons:SfRadioGroup x:Name="radioGroup">
<buttons:SfRadioButton x:Name="male" Text="Male"/>
<buttons:SfRadioButton x:Name="female" Text="Female" IsChecked="True"/>
</buttons:SfRadioGroup>
SfRadioGroup radioGroup = new SfRadioGroup();
SfRadioButton male = new SfRadioButton();
male.Text = "Male";
SfRadioButton female = new SfRadioButton();
female.IsChecked = true;
female.Text = "Female";
radioGroup.Children.Add(male);
radioGroup.Children.Add(female);
this.Content = radioGroup;
NOTE
SfRadioButtons are mutually exclusive when they are defined within SfRadioGroup.
You can find the complete getting started sample of the .NET MAUI Radio Button from this link.
NOTE
You can also explore our .NET MAUI Radio Button Example that shows you how to render the Radio Button in .NET MAUI.