讓我們想象一下,我有:綁定單選按鈕組在WPF
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton1IsChecked}" />
<RadioButton GroupName="Group1" IsChecked="{Binding Path=RadioButton2IsChecked}" />
然後在我的數據源I類有:
public bool RadioButton1IsChecked { get; set; }
public bool RadioButton2IsChecked { get; set; }
public enum RadioButtons { RadioButton1, RadioButton2, None }
public RadioButtons SelectedRadioButton
{
get
{
if (this.RadioButtonIsChecked)
return RadioButtons.RadioButton1;
else if (this.RadioButtonIsChecked)
return RadioButtons.RadioButton2;
else
return RadioButtons.None;
}
}
我能以某種方式直接與我的單選按鈕SelectedRadioButton
屬性?我真的需要RadioButton1IsChecked
和RadioButton2IsChecked
屬性只計算選定單選按鈕。
This [blog post](http://blogs.msdn.com/b/mthalman/archive/2008/09/04/wpf-data-binding-with-radiobutton.aspx)可能有幫助 – 2012-02-09 16:22:42
請參閱[我的回答在一個相關的問題(http://stackoverflow.com/questions/9145606/how-can-i-reduce-this-wpf-boilerplate-code/9145914#9145914),它應該幫助。 「SelectedItem」綁定到感興趣的屬性。 – 2012-02-09 18:39:42
我喜歡:http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum – quetzalcoatl 2012-08-08 13:28:58