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

Proposal to Remove Requirement of GroupName from RadioButtonGroup #21335

Open dainius-r opened 6 months ago

dainius-r commented 6 months ago

Description

When trying to use RadioButton selected value, binding to group property not works and currently is not usable. Using example from: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/radiobutton?view=net-maui-8.0#respond-to-radiobutton-state-changes

Steps to Reproduce

  1. Create standard MAUI project
  2. Add view xaml code:
    <StackLayout RadioButtonGroup.GroupName="{Binding GroupName}"
             RadioButtonGroup.SelectedValue="{Binding Selection}">
    <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>
  3. Add viewModel code:

    private object _groupName;
    public object GroupName
    {
      get => _groupName;
      set
      {
          _groupName = value;
          GetPropertyValue(nameof(GroupName));
      }
    }
    
    private object _selection;
    public object Selection
    {
      get => _selection;
      set
      {
          _selection = value;
          GetPropertyValue(nameof(Selection));
      }
    }
  4. Run project on Android

Link to public reproduction project repository

No response

Version with bug

8.0.10 SR3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13 - API 33

Did you find any workaround?

It only works when set RadioButtonGroup.GroupName to some static value

Relevant log output

No response

Zhanglirong-Winnie commented 6 months ago

Verified this issue with Visual Studio 17.10.0 Preview 2(8.0.10). Not repro on android platform with above code. Please check out the sample project I provided. MauiApp15 1.zip

dainius-r commented 6 months ago

@Zhanglirong-Winnie I have updated your project please see MainPage.xaml.cs line 15 MauiApp15.v2.zip

The key point is if you do not set GroupName = "anyTextValue", and leave it to null, then binding will not work for Selection propery and you will not get selected value. But if you will provide any text value to RadioButtonGroup.GroupName then selection binding works, maybe this is by design, but that is not specified in documentation

If this is be design that RadioButtonGroup.GroupName must have a value, that this issue can be closed.

Zhanglirong-Winnie commented 6 months ago

According to the user's latest comments and sample projects, this problem can be reproduced if the GroupName value is left empty. MauiApp15.v2.zip

dustin-wojciechowski commented 4 months ago

Hello @dainius-r, we've modified this as a proposal to allow for binding without a GroupName since it would take some changes to do. In the meanwhile, I've updated the documentation to bring attention to the necessity of having a GroupName for RadioButtonGroup. Thanks!