Force one or more checkboxes to be selected

醉酒当歌 提交于 2019-12-11 03:48:05

问题


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?


回答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.

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


来源:https://stackoverflow.com/questions/5971343/force-one-or-more-checkboxes-to-be-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!