我有三个复选框,它们有自己的错误检查,以检查它们是否有效,但我还想强制在继续之前必须至少检查一个。我目前正在使用 IDataErrorInfo 进行单个错误检查,并尝试使用 BindingGroups 来检查是否至少检查了一个但没有成功。
这是 XAML,
<StackPanel Orientation="Horizontal" Margin="5,2">
<Label Content="Checkboxes:" Width="100" HorizontalContentAlignment="Right"/>
<CheckBox Content="One" Margin="0,5">
<CheckBox.IsChecked>
<Binding Path="One" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule />
</Binding.ValidationRules>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<CheckBox Content="Two" Margin="5,5">
<CheckBox.IsChecked>
<Binding Path="Two" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule />
</Binding.ValidationRules>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<CheckBox Content="Three" Margin="0,5">
<CheckBox.IsChecked>
<Binding Path="Tree" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule />
</Binding.ValidationRules>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
以及后面的错误检查代码
public string this[string property]
{
get {
string result = null;
switch (property) {
case "One":
{
if (One) {
if (CheckValid(One)) {
result = "Invalid Entry";
}
}
}
break;
case "Two":
{
if (Two) {
if (CheckValid(Two)) {
result = "Invalid entry";
}
}
}
break;
case "Three":
{
if (Three) {
if (CheckValid(Three)) {
result = "Invalid entry"
}
}
}
break;
}
return result;
}
如果至少有一个未被选中,关于如何让复选框显示错误的任何建议?