views:

124

answers:

1

I am using a WPF tab control to present separate repeated instances of a user control. i.e. Tab1 for Item1 settings, Tab2 for Item2 settings, and so on.

It appears that the radio button group names are being shared between tabs. What is going on?

Simple example:

A window contains tabs. Each tab contains a user control.

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lib="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
    <TabControl Margin="0,0,0,100" Name="tabControl1">
        <TabItem Header="tabItem1" Name="tabItem1">
            <lib:UserControl1 x:Name="userControlInTab1" />
        </TabItem>
        <TabItem Header="tabItem2" Name="tabItem2">
            <lib:UserControl1 x:Name="userControlInTab2" />
        </TabItem>
    </TabControl>
</Grid>

The user control is simply two radiobuttons in a group:

<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="50" Width="100">
<StackPanel>
    <RadioButton GroupName="Group1" Name="radiobutton1" Content="option1" IsChecked="True" />
    <RadioButton GroupName="Group1" Name="radiobutton2" Content="option2" />
</StackPanel>

If you run this app, you will see that only the radiobutton1 in the second tab is checked, despite the usercontrol defining it to always be checked at startup.

Further, setting a radiobutton as checked in code behind seems to uncheck all the radiobuttons in other tabs!

It seems like things behave fine under mouse control (i.e. tabs are independent).

Lastly, the usercontrols do seem to be separate instantiations. I have tried this with sliders on user controls, for example, and they do behave independently across tabs. As they should.

Thanks for anyone's help with this. I have searched widely to no avail. Surely I'm not the only person who has had this issue. I'm using VS2008.

+1  A: 

Without the GroupName set it works. It is not strictly necessary since the RadioButtons in one container are automatically grouped anyway. For example:

<UserControl x:Class="WpfApplication1.UserControl1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="50" Width="100">
    <StackPanel>
        <StackPanel>
            <RadioButton  Name="radiobutton1" Content="option1" IsChecked="True" />
            <RadioButton  Name="radiobutton2" Content="option2" />
        </StackPanel>
        <StackPanel>
            <RadioButton  Name="radiobutton3" Content="option3" IsChecked="True" />
            <RadioButton  Name="radiobutton4" Content="option4" />
        </StackPanel>
    </StackPanel>
</UserControl>
Daniel Rose
The works. Thanks.So the groupname property seems entirely unnecessary, and you can always separate radiobuttons into containers...
Daniel
Exactly. You only would need GroupName if you want to group RadioButtons in the same container. But even then you could put them in an empty Border or similar.
Daniel Rose