【问题标题】:RadioButton binding in Windows Forms with INotifyPropertyChanged?带有 INotifyPropertyChanged 的​​ Windows 窗体中的 RadioButton 绑定?
【发布时间】:2011-03-10 19:33:19
【问题描述】:

我正在尝试将一些 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
}

有人知道如何实现这项工作吗?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

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

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

    【讨论】:

    • 这个问题有更新吗?看来我仍然可以重现 .NET 7.2 的问题。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2019-06-07
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多