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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要保留现有代码,您可以修改数据验证规则以同时检查所有三个复选框的状态。
并在一个值更改时通知所有三个复选框,因此当您取消选择最后一个复选框然后选择另一个复选框时,模型会清除验证错误。
To keep your existing code you can modify your data validation rule to check the state of all three checkboxes at the same time.
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.