dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.05k stars 1.73k forks source link

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

Open GuidoNeele opened 9 months ago

GuidoNeele commented 9 months ago

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

ghost commented 9 months ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

XamlTest commented 8 months ago

Verified this on Visual Studio Enterprise 17.9.0 Preview 2(8.0.3). Repro on Windows 11, Android 14.0-API34, iOS 17.0 and MacCatalyst with below Project:

RadioButtonDemos.zip

nnn149 commented 6 months ago

The Android platform doesn't work either

espenrl commented 5 days ago

It's not a platform thing. null value is explicitly excluded.

https://github.com/dotnet/maui/blob/979ad0280814557042103ec93aeb167c6180eb18/src/Controls/src/Core/RadioButton/RadioButtonGroupController.cs#L136-L147

And I don't know how this code would respond to a null value.

https://github.com/dotnet/maui/blob/979ad0280814557042103ec93aeb167c6180eb18/src/Controls/src/Core/RadioButton/RadioButton.cs#L426-L444