2011-04-21 45 views
0

我有兩個組合框的 - cbo_client_pay_method & cbo_terms停止有效性規則,如果組合框摺疊

其中cbo_client_pay_method項目(記帳)需要cbo_terms(30天等)是可見的其他公司瓦解了,我有這個功能已經在cbo_payment_type_SelectionChanged事件中設置。

我已經實現了一個驗證規則來測試cbo的非空& &如果selectedValue < 0(某些東西被選中),這工作正常。

這一切都很好,除非cbo的崩潰,驗證仍然會觸發!

如果元素被摺疊,我可以暫停validationRule嗎?

<StackPanel Name="sp_account" Orientation="Horizontal" VerticalAlignment="Center"> 
    <Label Content="Payment" Style="{StaticResource formLabel}"/> 
    <Grid> 
     <ComboBox Name="cbo_client_pay_method" Style="{StaticResource reminder_cbo}" SelectionChanged="cbo_client_payMethod_SelectionChanged" Validation.ErrorTemplate="{StaticResource validationTemplate}"> 
      <ComboBox.SelectedValue> 
       <Binding Path="client_payment_type_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"> 
        <Binding.ValidationRules> 
         <local:ValidCbo ErrorMessage="Select A Payment Type" /> 
        </Binding.ValidationRules> 
       </Binding> 
      </ComboBox.SelectedValue> 
     </ComboBox> 
     <TextBlock Name="txtSelectPayMethod" Text="Please Select A Payment Method..." Style="{StaticResource cbo_overlay}" /> 
    </Grid> 
</StackPanel> 

<StackPanel Name="sp_terms" Orientation="Horizontal" VerticalAlignment="Center"> 
    <Label Content="Terms" Style="{StaticResource formLabel}"/> 
    <Grid> 
     <ComboBox Name="cbo_terms" Style="{StaticResource reminder_cbo}" SelectionChanged="cbo_terms_SelectionChanged" Validation.ErrorTemplate="{StaticResource validationTemplate}"> 
      <ComboBox.SelectedValue> 
       <Binding Path="terms_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"> 
        <Binding.ValidationRules> 
         <local:ValidCbo ErrorMessage="Select Payment Terms" /> 
        </Binding.ValidationRules> 
       </Binding> 
      </ComboBox.SelectedValue> 
     </ComboBox> 
     <TextBlock Name="txtSelectTerms" Text="Please Select Payment Terms..." Style="{StaticResource cbo_overlay}" /> 
    </Grid> 
</StackPanel> 
public class ValidCbo : ValidationRule 
{ 
    private string _errorMessage; 
    public string ErrorMessage 
    { 
     get { return _errorMessage; } 
     set { _errorMessage = value; } 
    } 


    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    { 
     //if (this.ErrorMessage.Contains("Master") |) 
     if (value == null) 
     { 
      // value = null 
      return new ValidationResult(false, this.ErrorMessage); 
     } 
     else 
     { 
      // Not null 
      int selectedValue = (int)value; 
      if (selectedValue < 0) 
      { 
       return new ValidationResult(false, this.ErrorMessage); 
      } 
      else 
      { 
       return ValidationResult.ValidResult; 
      } 
     } 
    } 
} 

回答

1

您可以應用一個風格,只是如果ComboBox是可見的綁定值:

<ComboBox.Style> 
    <Style TargetType="{x:Type ComboBox}"> 
     <Style.Triggers> 
      <Trigger Property="Visibility" Value="Visible"> 
       <Setter Property="SelectedValue"> 
        <Setter.Value> 
         <Binding Path="terms_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"> 
          <Binding.ValidationRules> 
           <local:ValidCbo ErrorMessage="Select Payment Terms" /> 
          </Binding.ValidationRules> 
         </Binding> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</ComboBox.Style> 

您必須設置的SelectedValue的組合框本身或者它會覆蓋風格雖然。

+0

謝謝H.這正是我正在尋找的:-)順便說一句,我可以看到你編輯我的問題,並刪除了愚蠢的選項卡,我想這樣做,但我不知道如何,任何指針?謝謝 – 2011-04-22 18:15:13

+0

VisualStudio:Ctrl + E,D - > Autoformat | Alt +單擊+拖動鼠標 - >代碼塊選擇東西 – 2011-04-22 18:20:09

+0

我試圖在StackOverflow上做到這一點!歡呼的幫助 – 2011-04-24 00:22:46