Skip to content

RadioButton is broken #2081

@benstevens48

Description

@benstevens48

I know there is now a RadioButtons control, but this is about using multiple RadioButton controls, which is still necessary to support certain layouts (e.g. RadioButton controls in a WrapPanel).

Bug 1 - can't set IsChecked in XAML
Consider the following XAML.

     <StackPanel>
            <StackPanel BorderBrush="Black" BorderThickness="1">
                <RadioButton Content="Radio button 1" IsChecked="True"></RadioButton>
                <RadioButton Content="Radio button 2"></RadioButton>
                <RadioButton Content="Radio button 3"></RadioButton>
            </StackPanel>
            <StackPanel BorderBrush="Black" BorderThickness="1">
                <RadioButton Content="Radio button 1"></RadioButton>
                <RadioButton Content="Radio button 2"></RadioButton>
                <RadioButton Content="Radio button 3" IsChecked="True"></RadioButton>
            </StackPanel>
        </StackPanel>

RadioButton controls without a GroupName are scoped to their parent, thus this should produce two independent sets of radio buttons. Want to guess what it initially looks like when you load a page containing this markup? Only the final radio button is checked. The first one is not checked. It appears that the radio buttons are put into their respective groups after the IsChecked property is set in XAML. Note the the radio buttons do subsequently work as two separate groups. The workaround is that you have to set IsChecked in code behind after the InitializeComponent of the page, which is annoying.

Bug 2 - GroupName is scope to the entire visual tree rather than the naming container
I'm not sure if naming container is the right word - I think this is what it was called at some point, but maybe it's now called definition scope or something like that. Suppose you create a set of radio buttons inside a UserControl, like this.

<UserControl x:Class="App2.MyUserControl2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App2">
    <Grid>
        <StackPanel BorderBrush="Black" BorderThickness="1">
            <RadioButton Content="Radio button 1" GroupName="UserControlRadios"></RadioButton>
            <RadioButton Content="Radio button 2" GroupName="UserControlRadios"></RadioButton>
            <RadioButton Content="Radio button 3" GroupName="UserControlRadios"></RadioButton>
        </StackPanel>
    </Grid>
</UserControl>

Suppose you use two copies of the UserControl in your main page, like this.

        <StackPanel>
            <local:MyUserControl2></local:MyUserControl2>
            <local:MyUserControl2></local:MyUserControl2>
        </StackPanel>

How do you think the radio buttons will behave? You'd think you'd get two independent sets of 3 radio buttons. In fact, you will get a single set of 6 radio buttons. Now it's possible this behavior is deliberate, but I really don't think so - would anyone really expect it to work like this? This effectively makes setting the GroupName property to a fixed value useless. The workaround is to create a new GUID after InitializeComponent in the user control, and then to set the GroupName of all the radio buttons to that, and then set the IsChecked property of the one you want. Technically, fixing this so the GroupName is scoped to the current naming container or definition scope or whatever it's called, would be a breaking change. If you think people are really relying on the existing behavior (which I find extremely unlikely), then please consider adding a ScopedGroupName property to RadioButton which is scoped to the current naming container.

These bugs make working with RadioButton controls very awkward (and these bugs have been there from the beginning - I did report them on UserVoice in the past but that didn't get any attention).

Windows 10 version Saw the problem?
November 2019 Update (18363) Yes
Device form factor Saw the problem?
Desktop Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-RadioButtonsbugSomething isn't workingneeds-winui-3Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3)team-ControlsIssue for the Controls team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions