开发者

RadioButton binding in Windows Forms with INotifyPropertyChanged?

I am trying to bind some RadioButtons to booleans in a class which in-turn enable/disable other elements on the form. For example:

x radioButton1
    x checkBox1
x radioButton2
    x checkBox2

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

When I try to bind these it takes two clicks to change a RadioButton selection. It seems like the order of binds is causing a logic issue.

Here is code that shows this. The form is just two default named RadioButtons 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, Dat开发者_如何学运维aSourceUpdateMode.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
}

Anyone see a way to make this work?


This appears to be a bug and won't be fixed:

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

As a workaround, I "manually" hooked up the binding like this:

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


For me BT's answer was not enough. I had to add manual checking of which button was clicked, otherwise it still needed 2 clicks for 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;
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜