WPF中的多个验证规则和验证模板 [英] Multiple Validation Rules and Validation Templates in WPF

查看:131
本文介绍了WPF中的多个验证规则和验证模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有一种策略来处理多个验证规则和这些验证规则的模板.

Does anyone have a tactic for dealing with multiple validation rules and templates for those validation rules.

示例:

我想要两个验证规则(必填&数据)

I want to have two validation rules (required & data)

我想要...

根据要触发的规则,显示一个可以更改的模板 或者 根据触发的规则显示两个模板

One template that can change is display depending on which rule is fired or Two templates, which get displayed depending on which rule is fired

推荐答案

好,所以我想出了一种可行的方法,希望能从对此有兴趣的任何人那里得到反馈.

Ok, so I've figured out an approach that works and I was hoping to get feedback from anyone that might have an interest in this.

ValidationRule:

ValidationRule:

更改了我的验证规则,以发回具有IsRequired&邮件属性

My validation rule is altered to send back an "ErrorObject" that has IsRequired & Message properties

    public class ErrorObject
    {
        public bool IsRequired { get; set; }
        public string Message { get; set; }
    }

....

return new ValidationResult(false, new ErrorObject() { IsRequired = true, Message = "Is Required" });

模板:

现在,在验证模板中,我可以访问这些属性并相应地更改外观. (在此示例中,我在必填字段中显示*)

In the Validation Template I can now access these properties and alter the visual accordingly. (In this example I'm showing an * for required fields)

            <Border
                BorderBrush="Red"
                CornerRadius="3"
                BorderThickness="1">
                <AdornedElementPlaceholder
                    x:Name="errorAdorner" />
            </Border>
            <TextBlock
                Text="*"
                Foreground="Red"
                Visibility="{Binding ElementName=errorAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent.IsRequired, Converter={StaticResource BooleanToVisibilityConverter}}" />

因此,这是一个简单的示例,但是您可以想象它会变得非常强大.感谢MS让我们发送回一个对象!!!

So this is a simple example, but you can imagine that this can get very powerful. Thanks MS for letting send back an object!!!

这篇关于WPF中的多个验证规则和验证模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆