强制选中一个或多个复选框

时间:2011-05-11 22:29:51

标签: wpf checkbox idataerrorinfo

我有三个复选框,它们有自己的错误检查,检查它们是否有效,但我还想强制执行,在继续之前必须至少检查一个。我目前正在使用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;
    }

如果未选择至少一个错误,我是如何获取复选框以显示错误的?

1 个答案:

答案 0 :(得分:2)

要保留现有代码,您可以修改数据验证规则,以同时检查所有三个复选框的状态。

case "One":
  {
    if (One)
    {
      if (CheckValid(One))
      {
        result = "Invalid Entry";
      }
    }
    else if (!CheckThreeValid(One, Two, Three))
    {
      result = "Invalid entry";
    }
  }


private static bool CheckThreeValid(bool one, bool two, bool three)
{
  bool rc = true;
  if ( !one && !two && !three )
  {
    return false;
  }
  return rc;
}

并在一个值发生更改时通知所有三个CheckBox,因此当您取消选择最后一个CheckBox然后选择另一个复选框时,模型会清除验证错误。

public bool One
{ 
    get { return one; } 
    set 
    { 
        one = value;
        RaisePropertyChanged("One");
        RaisePropertyChanged("Two");
        RaisePropertyChanged("Three");
    } 
}