在名为 CarpetColor 的表单上创建一个属性,如下所示:
enum Color { Red, Blue, Green }
public Color CarpetColor
{
get { return rbGreen.Checked ? Green : (rbRed.Checked ? Red : Blue); }
set
{
if (value == Green)
rbGreen.Checked = true;
else
if (value == Red)
rbRed.Checked = true;
else
rbBlue.Checked = true;
}
}
然后您可以简单地将表单上的 CarpetColor 属性绑定到数据源的 CarpetColor。
我会让表单实现INotifyPropertyChanged 接口
并使用单选按钮事件来引发PropertyChanged事件
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
rbGreen.CheckedChanged += (s, args) => OnPropertyChanged("CarpetColor");
rbRed.CheckedChanged += (s, args) => OnPropertyChanged("CarpetColor");
rbBlue.CheckedChanged += (s, args) => OnPropertyChanged("CarpetColor");