C# Windows窗体中INotifyPropertyChanged的单选按钮绑定?
我试图将一些单选按钮绑定到类中的布尔值,从而启用/禁用表单上的其他元素。例如:C# Windows窗体中INotifyPropertyChanged的单选按钮绑定?,c#,winforms,C#,Winforms,我试图将一些单选按钮绑定到类中的布尔值,从而启用/禁用表单上的其他元素。例如: x radioButton1 x checkBox1 x radioButton2 x checkBox2 我只想在选择radioButton1时启用复选框1,同样地,对于radioButton2和复选框2也是如此 当我尝试绑定这些内容时,需要单击两下才能更改RadioButton选择。似乎绑定的顺序引起了一个逻辑问题 下面是显示这一点的代码。表单只是两个默认命名的单选按钮和两个复选框 public
x radioButton1
x checkBox1
x radioButton2
x checkBox2
我只想在选择radioButton1时启用复选框1,同样地,对于radioButton2和复选框2也是如此
当我尝试绑定这些内容时,需要单击两下才能更改RadioButton选择。似乎绑定的顺序引起了一个逻辑问题
下面是显示这一点的代码。表单只是两个默认命名的单选按钮和两个复选框
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
}
有人看到了实现此功能的方法吗?这似乎是一个bug,无法修复: 作为一种解决方法,我“手动”连接绑定,如下所示:
// 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));
对我来说,英国电信的回答是不够的。我必须添加手动检查哪个按钮被点击,否则它仍然需要点击两次单选按钮
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;
}
};
有关于这个问题的更新吗?看来我仍然可以用.NET7.2重现这个问题。