2011-05-11 40 views
2

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

的错误校验码我如何能得到相应的复选框以显示一个错误,如果没有选择至少一个什么建议吗?

回答

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

,当一个值发生变化,所以当你取消最后一个复选框,然后选择其他复选框模型清除验证错误通知所有三个复选框。

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