I had to put GroupName at the same level as SelectedValue like this:
<VerticalStackLayout RadioButtonGroup.GroupName="FOO" RadioButtonGroup.SelectedValue="{Binding SelectedThing}">
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I can't get RadioButtonGroup.SelectedValue to bind to a viewmodel property. I've read everything that you can google including:
and other questions asked here.
I have made a simplified example to eliminate extraneous issues. I don't get it, it should work and it should be simple based on documentation.
View
...
<Grid>
<ScrollView >
<VerticalStackLayout HorizontalOptions="Center" Padding="20,0,20,0" >
<Label Text="Select the course:"></Label>
<VerticalStackLayout RadioButtonGroup.SelectedValue="{Binding SelectedThing}">
<RadioButton GroupName="FOO" Content="ONE" Value="1"></RadioButton>
<RadioButton GroupName="FOO" Content="TWO" Value="2" ></RadioButton>
<RadioButton GroupName="FOO" Content="THREE" Value="3"></RadioButton>
</VerticalStackLayout>
<Label Text="{Binding SelectedThing, StringFormat='The selection is: {0} (not working!)'}"></Label>
...
ViewModel
(based on mvvm toolkit ObservableObject)
[ObservableProperty]
private object selectedThing;
I'm not sure what else might be relevant.
I had to put GroupName at the same level as SelectedValue like this:
<VerticalStackLayout RadioButtonGroup.GroupName="FOO" RadioButtonGroup.SelectedValue="{Binding SelectedThing}">
Hi @Crowcoder ,
My name is Harry, Support Engineer who specialize in UWP and MAUI. Thank you for reaching out on Microsoft Q&A!
I'm sorry to bother you, especially since you already know the cause of your issue. I'm sharing this answer to help others who might encounter the same problem can understand the issue.
In .NET MAUI, RadioButtonGroup
is a feature that allows you to group multiple RadioButton
controls together so that only one can be selected at a time. The SelectedValue
property is used to bind the selected option to a property in your ViewModel, making it easy to track user choices in an MVVM-friendly way.
The problem in your original setup was that the GroupName
was applied individually to each RadioButton
, while the SelectedValue
was set on the container (like VerticalStackLayout
). For the binding to work correctly, both GroupName
and SelectedValue
need to be declared at the same level—on the container that wraps the RadioButton
elements. Once you moved the GroupName
to the container alongside SelectedValue
, the binding worked as expected.
Here is the full correction of your code:
<VerticalStackLayout
RadioButtonGroup.SelectedValue="{Binding SelectedThing}"
RadioButtonGroup.GroupName="FOO">
<RadioButton Content="ONE" Value="1"></RadioButton>
<RadioButton Content="TWO" Value="2" ></RadioButton>
<RadioButton Content="THREE" Value="3"></RadioButton>
</VerticalStackLayout>
You can read this documentation to learn more. Thank you!