I am sure that this behavior is known, but I can not use it. I have the following code:
<Window x:Class="ContentControlListDataTemplateKacke.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <DockPanel> <TabControl ItemsSource="{Binding Items}"> <TabControl.ContentTemplate> <DataTemplate> <StackPanel> <Label Content="{Binding Name}" /> <RadioButton Content="Option1" IsChecked="{Binding Option1}" /> <RadioButton Content="Option2" IsChecked="{Binding Option2}" /> </StackPanel> </DataTemplate> </TabControl.ContentTemplate> </TabControl> </DockPanel> </Window>
Simple code:
public partial class MainWindow { public MainWindow() { InitializeComponent(); DataContext = new ViewModel(); } }
ViewModel looks like this:
public class ViewModel : NotificationObject { public ViewModel() { Items = new ObservableCollection<Item> { new Item {Name = "1", Option1 = true}, new Item {Name = "2", Option2 = true} }; } public ObservableCollection<Item> Items { get; set; } }
And an element like this:
public class Item : NotificationObject { public string Name { get; set; } private bool _option1; public bool Option1 { get { return _option1; } set { _option1 = value; RaisePropertyChanged(() => Option1); } } private bool _option2; public bool Option2 { get { return _option2; } set { _option2 = value; RaisePropertyChanged(() => Option2); } } }
I use Prism, so RaisePropertyChanged raises the PropertyChanged event. Select the second tab, then the first tab, then the second tab and the voilá button, and RadioButtons on the second tab will not be selected.
Why?
Other solution besides Rachels
My colleague had an idea to associate the GroupName RadioButtons property with a unique string for each item. Just change the RadioButtons declaration to this:
<RadioButton GroupName="{Binding Name}" Content="Option1" IsChecked="{Binding Option1}" /> <RadioButton GroupName="{Binding Name}" Content="Option2" IsChecked="{Binding Option2}" />
And it works if the Name property is unique to all elements (as for my problem).
source share