2011-05-11 39 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"); 
    } 
} 
相關問題