Associating RadioButton in Windows Forms with INotifyPropertyChanged?

I am trying to associate some RadioButtons with booleans in the class, which in turn allow you to enable / disable other elements in the form. For instance:

x radioButton1 x checkBox1 x radioButton2 x checkBox2 

I want to enable checkBox1 only when radioButton1 is selected and similarly for radioButton2 and checkBox2.

When I try to link them, it takes two clicks to change the selection of RadioButton. The order of the bindings seems to be causing a logical problem.

Here is the code that shows this. The form represents only two default RadioButtons names and two CheckBoxes.

 public partial class Form1 : Form { public Form1() { InitializeComponent(); BindingSource bindingSource = new BindingSource(new Model(), ""); radioButton1.DataBindings.Add(new Binding("Checked", bindingSource, "rb1Checked", true, DataSourceUpdateMode.OnPropertyChanged)); radioButton2.DataBindings.Add(new Binding("Checked", bindingSource, "rb2Checked", true, DataSourceUpdateMode.OnPropertyChanged)); checkBox1.DataBindings.Add(new Binding("Enabled", bindingSource, "cb1Enabled", true, DataSourceUpdateMode.OnPropertyChanged)); checkBox2.DataBindings.Add(new Binding("Enabled", bindingSource, "cb2Enabled", true, DataSourceUpdateMode.OnPropertyChanged)); } } public class Model : INotifyPropertyChanged { private bool m_rb1Checked; public bool rb1Checked { get { return m_rb1Checked; } set { m_rb1Checked = value; NotifyPropertyChanged("cb1Enabled"); } } private bool m_rb2Checked; public bool rb2Checked { get { return m_rb2Checked; } set { m_rb2Checked = value; NotifyPropertyChanged("cb2Enabled"); } } public bool cb1Enabled { get { return rb1Checked; } } public bool cb2Enabled { get { return rb2Checked; } } public Model() { rb1Checked = true; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string fieldName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(fieldName)); } } #endregion } 

Does anyone see a way to make this work?

+4
source share
2 answers

This seems like a bug and will not be fixed:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5735dac2-63e9-4797-80af-91969bf4d16e/

As a workaround, I manually connected the binding as follows:

 // Set initial values radioButton1.Checked = model.Checked; radioButton2.Checked = model.Checked; // Change on event radioButton1.CheckedChanged += delegate { model.rb1Checked = radioButton1.Checked; }; radioButton2.CheckedChanged += delegate { model.rb2Checked = radioButton2.Checked; }; // These stay the same checkBox1.DataBindings.Add(new Binding("Enabled", bindingSource, "cb1Enabled", true, DataSourceUpdateMode.OnPropertyChanged)); checkBox2.DataBindings.Add(new Binding("Enabled", bindingSource, "cb2Enabled", true, DataSourceUpdateMode.OnPropertyChanged)); 
+6
source

BT's answer was not enough for me. I had to add a manual check which button is pressed, otherwise 2 clicks are still needed for the radio exchange.

 this.ViewModel = new FancyClassViewModel(); this.radioButton1.CheckedChanged += delegate { this.ViewModel.Radio1Checked = this.radioButton1.Checked; }; this.radioButton2.CheckedChanged += delegate { this.ViewModel.Radio2Checked = this.radioButton2.Checked; }; this.ViewModel.PropertyChanged += (sender, e) => { if (e.PropertyName == "Radio1") { this.radioButton1.Checked = this.ViewModel.Radio1Checked; } if (e.PropertyName == "Radio2") { this.radioButton2.Checked = this.ViewModel.Radio2Checked; } }; 
0
source

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


All Articles