的Windows窗体与INotifyPropertyChange

的Windows窗体与INotifyPropertyChange

本文介绍了单选按钮的Windows窗体与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

我想只有当radioButton2和checkBox2选择radioButton1,同样使checkBox1。

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

任何人都明白的方式,使这项工作?

Anyone see a way to make this work?

推荐答案

这似乎是一个错误,将无法修复

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

作为一种变通方法,我手动迷上了这样的绑定:

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

这篇关于单选按钮的Windows窗体与INotifyPropertyChanged的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:26