【问题标题】:Stop ValidationRule if ComboBox is Collapsed如果 ComboBox 已折叠,则停止 ValidationRule
【发布时间】:2026-02-12 05:20:10
【问题描述】:

我有两个组合框 - cbo_client_pay_method 和 cbo_terms

其中一个 cbo_client_pay_method 项目(在帐户中)需要 cbo_terms(30 天等...)可见,否则它已折叠,我已经在 cbo_payment_type_SelectionChanged 事件中设置了此功能。

我已经实现了一个验证规则,用于测试 cbo 是否不为空 && 如果 selectedValue

这一切都很好,除非 cbo 被折叠,验证仍然触发!

如果元素折叠,我可以停止验证规则吗?

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

【问题讨论】:

    标签: wpf validation


    【解决方案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,否则它将覆盖样式。

    【讨论】:

    • 谢谢 H.B.这正是我要找的:-)顺便说一句,我可以看到你编辑了我的问题并删除了愚蠢的标签,我想这样做但我不知道怎么做,任何指针?谢谢
    • VisualStudio: Ctrl + E, D -> 自动格式化 | Alt + 单击 + 拖动鼠标 -> 代码块选择事物
    • 我试图在 * 上做到这一点!为帮助干杯
    • @VasilijeBursac:也许您可以在控件折叠时触发验证,或者手动删除错误。那里的方法Validation.ClearInvalid 可能会有所帮助。我无法给出更具体的建议,因为我之前没有遇到过这个问题(而且这些天我很少使用 WPF)。
    • @VasilijeBursac:我不知道如何触发验证,也许你可以在某个地方找到答案。如前所述,我会尝试查看 Validation.ClearInvalid 方法,它应该可以消除错误。