首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果ComboBox已折叠,则停止ValidationRule

如果ComboBox已折叠,则停止ValidationRule
EN

Stack Overflow用户
提问于 2011-04-21 18:55:15
回答 1查看 849关注 0票数 1

我有两个组合框- cbo_client_pay_method和cbo_terms

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

我已经实现了一个测试cbo是否不为空的validationRule &&如果cbo <0 (选择了某项内容),则可以正常工作。

这一切都很好用,除非cbo崩溃,验证仍然会触发!

如果元素被折叠,我可以停止validationRule吗?

代码语言:javascript
运行
复制
<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>
代码语言:javascript
运行
复制
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;
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-21 21:37:51

您可以应用仅在ComboBox可见时才绑定值的样式:

代码语言:javascript
运行
复制
<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,否则它会覆盖样式。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5743127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档