如果ComboBox被折叠,则停止ValidationRule

时间:2011-04-21 10:55:15

标签: wpf validation

我有两个ComboBox --cbo_client_pay_method& cbo_terms

其中一个cbo_client_pay_method项目(在帐户上)要求cbo_terms(30天等等)可见,否则其崩溃,我已在cbo_payment_type_SelectionChanged事件中设置此功能。

我已经实现了一个validationRule,用于测试cbo是否为null&&如果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 个答案:

答案 0 :(得分: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>

您必须在ComboBox本身中设置SelectedValue,否则它将覆盖该样式。

相关问题