Mouse On Mars
Mouse On Mars

Reputation: 1132

WPF Radio Button Show Wrong Group Binding Behavior

I am using the .NET Framework 4.5 and observe some strange behavior when I use several RadioButton Controls within a Grid. I would expect to be able to select and deselect each RadioButton individually since I did not use the GroupName property to bind the RadioButtons together. However, I can only select one RadioButton at a time. How can I suppress this behavior? Here is a minimal example:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <RadioButton Grid.Row="0" Grid.Column="0" ></RadioButton>
        <RadioButton Grid.Row="1" Grid.Column="0" ></RadioButton>
        <RadioButton Grid.Row="2" Grid.Column="0" ></RadioButton>
    </Grid>

Upvotes: 0

Views: 99

Answers (1)

BolletuH
BolletuH

Reputation: 124

Technically this is expected behavior, but not in the way you may think. GroupName is a property that expects nothing more than a mere string and actually defaults to string.Empty or "". There is actually a value for the group it's just an empty string is all, and WPF processes them as being a part of the same grouping. From Microsoft:

The name of the group that the radio button belongs to. The default is an empty string ("").

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobutton.groupname(v=vs.110).aspx

P.S. Wouldn't checkboxes be more appropriate for this type of situation?

Upvotes: 3

Related Questions