Windows窗体中的RadioButton绑定与INotifyPropertyChanged?

我试图将一些RadioButtons绑定到一个类中的布尔值,然后启用/禁用表单上的其他元素。 例如:

x radioButton1 x checkBox1 x radioButton2 x checkBox2 

我想只在选择了radioButton1时启用checkBox1,同样也支持radioButton2和checkBox2。

当我尝试绑定这些时,需要两次单击才能更改RadioButton选择。 似乎绑定的顺序导致逻辑问题。

这是代码,显示了这一点。 表单只有两个默认名为RadioButtons和两个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 } 

有人看到了让这项工作的方法吗?

这似乎是一个错误,不会修复:

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

作为一种解决方法,我“手动”连接绑定,如下所示:

 // 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)); 

对我来说BT的答案还不够。 我必须添加手动检查单击了哪个按钮,否则仍然需要2次单击radiobuttons。

 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; } };