1
votes

I have multiple ratio buttons in itemscontrols and datatemplates binded to database data in a MVVM / prism application. Each group of radio buttons is grouped with a name accordingly so that they are seprate groups.

The problem I'm having (and goes against convention of radio buttons) is that you can select multiple options within the group. Not all options allow multiple selection. Some behave as they should others do not. On inspection through snoop all radio buttons are part of the same group but with multiple buttons reporting true to IsChecked.

Any ideas?

Thanks

EDIT - Code

XAML

   <StackPanel Grid.Column="0" Margin="10,0,0,10">
                                        <TextBlock Margin="5,5,0,5"
                                                   FontSize="16"
                                                   FontWeight="Bold"
                                                   Foreground="{Binding      Path=ThemeBackground}"
                                                   Text="From" />
                                        <ItemsControl ItemsSource="{Binding Path=InternetItems}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <RadioButton Margin="5"
                                                                 Content="{Binding Path=Title}"
                                                                 GroupName="InternetFrom"
                                                                 IsChecked="{Binding Path=IsSelected}"
                                                                 IsEnabled="{Binding Path=IsEnabled}" />
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>

View Model

public ObservableCollection<Item> InternetItems
    {
        get
        {
            return
                new ObservableCollection<Item>(
                    _items.Where(x => x.Category == Category.InternetFrom).OrderBy(x => x.DisplayOrder));
        }

    }

Edit -

Problem resolved. Code behind was initiating a new observable collection each time a radio button was selected leading to multiple datacontexts regardless of the groupnames of the radio buttons being the same

2
post the relevant code and XAML.Federico Berasategui

2 Answers

3
votes

RadioButton controls are mutually excusive if they share the same container (e.g. anything derives from Panel, or ContentControl). In your case, each item generated in your ItemsControl is a separate container thus the buttons are not automatically mutually exclusive.

For example: If your ItemsControl is setup like this, the buttons are mutually exclusive:

<ItemsControl>
    <RadioButton Content="1" />
    <RadioButton Content="2" />
    <RadioButton Content="3" />
    <RadioButton Content="4" />
</ItemsControl>

But this is not:

<ItemsControl>
    <Grid>
        <RadioButton Content="1" />
    </Grid>
    <Grid>
        <RadioButton Content="2" />
    </Grid>
    <Grid>
        <RadioButton Content="3" />
    </Grid>
    <Grid>
        <RadioButton Content="4" />
    </Grid>
</ItemsControl>

As Dean said, assigning the same GroupName property will solve your problem.

<ItemsControl>
    <Grid>
        <RadioButton Content="1"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="2"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="3"
                        GroupName="Group1" />
    </Grid>
    <Grid>
        <RadioButton Content="4"
                        GroupName="Group1" />
    </Grid>
</ItemsControl>

Edit

If you have multiple ItemsControls, you can simply set a different GroupName for RadioButtons in each ItemsControl. An in-scope default style comes in handy in this case:

<StackPanel>
    <ItemsControl>
        <ItemsControl.Resources>
            <Style TargetType="{x:Type RadioButton}">
                <Setter Property="GroupName"
                        Value="Group1" />
            </Style>
        </ItemsControl.Resources>
        <Grid>
            <RadioButton Content="1" />
        </Grid>
        <Grid>
            <RadioButton Content="2" />
        </Grid>
        <Grid>
            <RadioButton Content="3" />
        </Grid>
        <Grid>
            <RadioButton Content="4" />
        </Grid>
    </ItemsControl>
    <ItemsControl>
        <ItemsControl.Resources>
            <Style TargetType="{x:Type RadioButton}">
                <Setter Property="GroupName"
                        Value="Group2" />
            </Style>
        </ItemsControl.Resources>
        <Grid>
            <RadioButton Content="1" />
        </Grid>
        <Grid>
            <RadioButton Content="2" />
        </Grid>
        <Grid>
            <RadioButton Content="3" />
        </Grid>
        <Grid>
            <RadioButton Content="4" />
        </Grid>
    </ItemsControl>
</StackPanel>
2
votes

You're doing something wrong, RadioButtons within a same group are mutually exclusive. If you use GroupName Property to assign two or more RadioButtons to a group you will be able to select only one RadioButton from that group.

RadioButton.GroupName Property

Gets or sets the name that specifies which RadioButton controls are mutually exclusive.

The user can choose one RadioButton in each group.

Do you really think you discovered such a fundamental bug in the .NET Framework that no one reported yet and was not fixed already?