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?
source share