The RadioButton IsChecked property is overridden when tabs change

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).

+4
source share
2 answers

WPF reads all RadioButtons as part of the same group, and only one item can be selected in the radio button group.

Download Order:

  • Download tab
  • Download Tab1.Radio1. IsChecked = True
  • Download Tab1.Radio2. IsChecked = True, so set Tab1.Radio2.IsChecked = False

  • Click tab 2

  • Download tab2
  • Download Tab2.Radio1. IsChecked = True, so set Tab1.Radio2.IsChecked = False
  • Download Tab2.Radio2. IsChecked = True, so set Tab2.Radio1.IsChecked = False

  • Currently Tab2.Radio2 is checked only one, and all other radio stations have been downloaded and removed, so their DataBound values ​​are updated to false.

  • Click tab 1

  • Download Tab1.Radio1. IsChecked = False
  • Download Tab1.Radio2. IsChecked = False

If you use radio buttons without binding and both can be checked at the same time, I would suggest switching to CheckBoxes

If they need to be grouped, and only one item can be selected at a time, I would suggest switching to the ListBox drawn with RadioButtons and saving only SelectedOption in the ViewModel

Here is the style that I usually use for this:

 <Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}"> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" /> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style TargetType="{x:Type ListBoxItem}" > <Setter Property="Margin" Value="2, 2, 2, 0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border Background="Transparent"> <RadioButton Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center" IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> </Style> 

It is used as follows:

 <ListBox ItemsSource="{Binding Options}" SelectedValue="{Binding SelectedValue}" Style="{StaticResource RadioButtonListBoxStyle}" /> 
+1
source

I had a problem very similar to the one where the text was cleared when I switched between views. If I remember correctly, it worked.

  • Bind OneWay to properties and mark these related properties with an attribute.
  • Each time you load a view (and therefore viewmodel), use reflection for the above attribute to find related properties.
  • Turn off the PropertyChanged event for each of the properties to properly refresh the view.

I think this is the result of loading by default with the default settings and does not request properties at startup, since nothing raises the PropertyChanged event.

Also, this is not part of your question, but you can directly set the data context in XAML (via the DataContext property in Window) so that Visual Studio does not need an explicit codebehind file.

0
source

All Articles