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

发布于 2024-11-06 06:03:09 字数 2675 浏览 9 评论 0原文

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

如果至少没有选择一个复选框,有什么建议可以让复选框显示错误吗?

I have three checkboxes that have their own error checking as to whether them being checked is valid but I would also like to enforce that at least one must be checked before continuing. I'm currently using IDataErrorInfo for the individual error checking and have tried using BindingGroups to check that at least one is checked with no success.

Here's the 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>

And the error checking code behind

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

Any suggestion on how I can get the checkboxes to display an error if at least one is not selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热鲨 2024-11-13 06:03:09

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

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

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

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

To keep your existing code you can modify your data validation rule to check the state of all three checkboxes at the same time.

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

and notify all three CheckBoxes when one value changes so when you deselect the last CheckBox and then select another checkbox the model clears the validation error.

public bool One
{ 
    get { return one; } 
    set 
    { 
        one = value;
        RaisePropertyChanged("One");
        RaisePropertyChanged("Two");
        RaisePropertyChanged("Three");
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文