Skip to content

RadioButtonGroup.SelectedValue = null doesn't reset RadioButton IsChecked state #19437

@GuidoNeele

Description

@GuidoNeele

Description

<StackLayout Margin="10"
             RadioButtonGroup.GroupName="{Binding GroupName}"
             RadioButtonGroup.SelectedValue="{Binding Selection}">
        <Button Command="{Binding ResetCommand}" Text="Reset" />
        <Label Text="What's your favorite animal?" />

        <RadioButton Content="Cat" Value="Cat" />
        <RadioButton Content="Dog" Value="Dog" />
        <RadioButton Content="Elephant" Value="Elephant" />
        <RadioButton Content="Monkey" Value="Monkey" />

        <Label x:Name="animalLabel">
            <Label.FormattedText>
                <FormattedString>
                    <Span Text="You have chosen:" />
                    <Span Text="{Binding Selection}" />
                </FormattedString>
            </Label.FormattedText>
        </Label>
</StackLayout>
public class AnimalViewModel : INotifyPropertyChanged
{
    string groupName;
    object selection;

    public AnimalViewModel()
    {
        ResetCommand = new Command(() =>
        {
            Selection = null;
        });
    }

    public string GroupName
    {
       get => groupName;
       set
       {
           groupName = value;
           OnPropertyChanged(nameof(GroupName));
       }
    }

    public object Selection
    {
        get => selection;
        set
        {
            selection = value;
            OnPropertyChanged(nameof(Selection));
        }
    }

    public ICommand ResetCommand { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

When Selection is set to null an animal stays checked. It should remove the checked state. If the Value of the RadioButton is not equal to the RadioButtonGroup.SelectedValue.

Select an animal:
image

After SelectedValue is set to null:
image

Steps to Reproduce

No response

Link to public reproduction project repository

Used this repo to add a ResetButton to the GroupedRadioButtonsViewModelPage sample page
https://github.com/dotnet/maui-samples/tree/main/8.0/UserInterface/Views/RadioButtonDemos

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

net8.0-windows10.0.19041.0

Did you find any workaround?

I've put this code in the code behind file of the view.

vm.PropertyChanged += (s, e) =>
{
    if (e.PropertyName == nameof(vm.SelectedCountryCode))
    {
        if (vm.SelectedCountryCode == null)
        {
            foreach (var child in this.Countries.Children)
            {
                if (child is RadioButton radioButton)
                {
                    radioButton.IsChecked = false;
                }
            }
        }
    }
};

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions