像往常一样绑定属性
<CheckBox Name="check1" IsChecked="{Binding Path=myViewModelProperty}" />
<CheckBox Name="check2" IsChecked="{Binding Path=myViewModelProperty}" />
<CheckBox Name="check3" IsChecked="{Binding Path=myViewModelProperty}" />
<CheckBox Name="check4" IsChecked="{Binding Path=myViewModelProperty}" />
然后,在 ViewModel 中:
public Boolean myViewModelProperty
{
get { ... }
set { ... }
}
同样在 ViewModel 中,需要时:
this.RaisePropertyChanged<Boolean>(() => this.myViewModelProperty);
如果需要,您可以在一个属性上绑定尽可能多的元素值(即使我不理解具有相同属性的 4 个复选框^^)
MultiBinding 用于将一个元素值绑定到许多 ViewModel 属性。
编辑:考虑到必须将 4 个复选框转换为枚举的事实:
<CheckBox Name="check1" IsChecked="{Binding Path=myViewModelProperty1}" />
<CheckBox Name="check2" IsChecked="{Binding Path=myViewModelProperty2}" />
<CheckBox Name="check3" IsChecked="{Binding Path=myViewModelProperty3}" />
<CheckBox Name="check4" IsChecked="{Binding Path=myViewModelProperty4}" />
public Boolean myViewModelProperty1
{
get
{
return this.b1;
}
set
{
this.b1 =value;
this.EvaluateMyEnum();
}
}
public Boolean myViewModelProperty2
{
get
{
return this.b2;
}
set
{
this.b2 =value;
this.EvaluateMyEnum();
}
}
public Boolean myViewModelProperty3
{
get
{
return this.b2;
}
set
{
this.b2 =value;
this.EvaluateMyEnum();
}
}
public Boolean myViewModelProperty4
{
get
{
return this.b4;
}
set
{
this.b4 =value;
this.EvaluateMyEnum();
}
}
void EvaluateMyEnum()
{
this.MyViewModelEnum = ......;
this.RaisePropertyChanged<MyEnum>(() => this.myViewModelEnum);
}