0
votes

I have a tab control bound to an observable collection. Each tab displays a single radio button group. When the tab is changed, for some reason it deselects the selected radio button.

Each item in the observable collection stores the selected radio button for its own tab. I believe this means they can't interfere with each other. I also recognize that only one radio button can be selected per group. Yet, each tab is in its own group. Why is the selected radio button being changed when the tab changes?

XAML

<TabControl x:Name="MainTabControl" >
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TabName}"></TextBlock>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <RadioButton x:Name="RadioButtonX"
                             Grid.Column="0"
                             VerticalAlignment="Center"
                             Content="X"
                             IsChecked="{Binding XChecked}" />
                <RadioButton x:Name="RadioButtonY"
                             Grid.Column="1"
                             VerticalAlignment="Center"
                             Content="Y"
                             IsChecked="{Binding YChecked}" />
                <RadioButton x:Name="RadioButtonZ"
                             Grid.Column="2"
                             VerticalAlignment="Center"
                             Content="Z"
                             IsChecked="{Binding ZChecked}" />
            </Grid>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

C#

public partial class MainWindow
{
    public ObservableCollection<RadioButtonSettings> listOfButtonSettings { get; set; }

    public MainWindow()
    {
        listOfButtonSettings = new ObservableCollection<RadioButtonSettings>
        {
            new RadioButtonSettings {TabName = "tab1"},
            new RadioButtonSettings {TabName = "tab2"},
            new RadioButtonSettings {TabName = "tab3"}
        };

        InitializeComponent();
       MainTabControl.ItemsSource = listOfButtonSettings;
    }
}

public class RadioButtonSettings
{
    public string TabName { get; set; }
    public bool XChecked { get; set; }
    public bool YChecked { get; set; }
    public bool ZChecked { get; set; }

    public RadioButtonSettings()
    {
        XChecked = true;
    }
}
2

2 Answers

1
votes

This issue is described here

The workaround is to use the RadioButtonExtended class as written in the above link. You can then change your XAML to the following and it will work as expected:

 <RadioButtonExtended x:Name="RadioButtonX"
     Grid.Column="0"
     VerticalAlignment="Center"
     Content="X"
     IsCheckedReal="{Binding XChecked}" />
 <RadioButtonExtended x:Name="RadioButtonY"
     Grid.Column="1"
     VerticalAlignment="Center"
     Content="Y"
     IsCheckedReal="{Binding YChecked}" />
 <RadioButtonExtended x:Name="RadioButtonZ"
     Grid.Column="2"
     VerticalAlignment="Center"
     Content="Z"
     IsCheckedReal="{Binding ZChecked}" />
-1
votes

I can say that the names of the radio buttons are the same for each tab. So, when you set Y on tab2, X will be unchecked on tab2, but because it has same name on tab1, will be also unchecked! Also, if you select on tab1, the Y checkbox, when you change the tab, Y will be automatically unchecked, because on each tab, at first, X is selected!

You need a unique name for your checkboxes. But, as far as I know, there is no method to bind the name to a property. In conclusion, my personal opinion is that you have to create each tab control separately, and give unique names to the checkboxes.

Hope it helps!