Related problems with RadioButton

I have a seemingly simple task that gives me headaches and will be very grateful for the help.

I want to bind two booleans to the IsChecked property of two RadioButton s using the same GroupName (so only one of them is checked at a time).

The problem I am facing is that when the ContentPresenter Content is about to change (via binding to the SelectedItem ComboBox), the current content gets a call to the setter property with the value of the same property, but from the view model, which is about to will become new content. (!) The end result is a change in the presentation model, despite the absence of a click on the RadioButton associated with the property in question.

I put together an example application that shows the problem. To play, run the application and follow these steps:

  • Select "One" in the combobox => MyPropery field, MyProperty2 - no.
  • Select "Three" in the combobox => MyPropery field, MyProperty2 - no.
  • If you select Three, choose MyProperty2> MyProperty2 (also indicated in the debug output window).
  • Select "One" in the combobox => MyPropery field, MyProperty2 - no. Notice how the debug window shows how MyProperty2 of the Three object is set to false here.
  • Select "Three" in combobox => Now none of the Radiobuttons are tested (due to # 4).

If between # 3 and # 4 you first select “Two” in the drop-down list so that ContentPresenter displays a different view (selected using the DataTemplate), the problem does not appear !?

Can someone explain why the property was set in step 4 when exchanging views using ContentPresenter and what can be done with it?

+4
source share
2 answers

DataTemplates are cached, so both One and Three use the same UserControl . You can verify this by adding the Loaded event and switching between options to the Loaded control.

When you switch to Two and return to Three , WPf just redraws the item from the cache, but switching to One and back to Three , it changes the DataContext behind the object, I think this causes a problem, because it seems to clear the second RadioButton IsChecked before delete the DataContext, so the end result is that property2 gets false. This does not happen if the two buttons One and Three are selected.

Usually in such a situation, I will have a VM containing ObservableCollection<T> Items and int SelectedIndex . I will then draw the user interface using a ListBox that has been rewritten to use RadioButtons for items. Thus, you can select only one item at a time, and only one property exists to store the selected item.

+1
source

This seems to be a bug (ContentPresenter content is changing, but DataContext does not).

But I can offer you a solution. In your example, do the following (MainWindow.xaml):

 <StackPanel> <ComboBox ItemsSource="{Binding MyItems, Mode=OneWay}" Name="cmbBox" DisplayMemberPath="ID" SelectionChanged="cmbBox_SelectionChanged"/> <ContentPresenter Name="cp1" /> </StackPanel> 

In cmbBox_SelectionChanged, the following looks:

 private void cmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { cp1.Content = null; cp1.DataContext = null; cp1.Content = cmbBox.SelectedItem; } 

This solves the problem (not a great solution, but a workaround).

0
source

Source: https://habr.com/ru/post/1399181/


All Articles