【问题标题】:Force one or more checkboxes to be selected强制选择一个或多个复选框
【发布时间】:2026-02-10 10:15:01
【问题描述】:

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

如果至少有一个未被选中,我如何让复选框显示错误?

【问题讨论】:

    标签: wpf checkbox idataerrorinfo


    【解决方案1】:

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

    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");
        } 
    }
    

    【讨论】:

      最近更新 更多