2011-03-10 47 views
4

我正試圖將一些RadioButtons綁定到類中的布爾值,並反過來啓用/禁用窗體上的其他元素。例如:使用INotifyPropertyChanged在Windows窗體中進行RadioButton綁定?

x radioButton1 
    x checkBox1 
x radioButton2 
    x checkBox2 

我想只有當radioButton2和checkBox2選擇radioButton1,同樣使checkBox1。

當我嘗試綁定這些時,需要兩次點擊才能更改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 
} 

任何人都看到一種方法來使這項工作?

回答

6

這似乎是一個錯誤,不是固定不變:

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

對我來說英國電信的答案還不夠。我不得不添加手動檢查哪個按鈕被點擊,否則它仍然需要點擊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; 
    } 
}; 
相關問題