How to get the selected .NET MAUI RadioButton (SfRadioButton)?
This article explains how to get the selected .NET MAUI RadioButton from RadioGroup. You can get the selected RadioButton in the RadioGroup by using the CheckedItem property of RadioGroup as per the below code sample.
XAML:
<ContentPage.BindingContext>
<local:ViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<buttons:SfRadioGroup x:Name="radioGroup"
BindableLayout.ItemsSource="{Binding Cities}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<buttons:SfRadioButton
Text="{Binding CityName}" />
</DataTemplate>
</BindableLayout.ItemTemplate>
</buttons:SfRadioGroup>
<Button WidthRequest="200" Text="Get Checked RadioButton" Clicked="Button_Clicked"/>
</StackLayout>
C#:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
var selectedRadioButton = radioGroup.CheckedItem.Text;
App.Current.MainPage.DisplayAlert("Hello", "Selected " + selectedRadioButton, "Ok");
}
}
public class City
{
public string CityName { get; set; }
}
public class ViewModel
{
private ObservableCollection<city> cities;
public ObservableCollection<city> Cities
{
get { return cities; }
set { cities = value; }
}
public ViewModel()
{
Cities = new ObservableCollection<city>();
Cities.Add(new City() { CityName = "Tokyo" });
Cities.Add(new City() { CityName = "Paris" });
Cities.Add(new City() { CityName = "New York City" });
Cities.Add(new City() { CityName = "Singapore" });
Cities.Add(new City() { CityName = "Vienna" });
Cities.Add(new City() { CityName = "Sydney" });
Cities.Add(new City() { CityName = "Zurich" });
Cities.Add(new City() { CityName = "Toronto" });
Cities.Add(new City() { CityName = "Copenhagen" });
Cities.Add(new City() { CityName = "Melbourne" });
Cities.Add(new City() { CityName = "Dubai" });
}
}
Output:
Conclusion:
I hope you enjoyed learning how to create a Radio Button group in .NET MAUI Radio Button (SfRadioButton).
Refer to our .NET MAUI RadioButton’s feature tour page for other groundbreaking feature representations. You can explore our .NET MAUI RadioButton documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our .NET MAUI RadioButton and other .NET MAUI components.
Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!