原文:数据绑定(九)Binding的数据校验

Binding用ValidationRules属性来校验数据的有效性,ValidationRules属性类型是Collection<ValidationRule>,他可以设置多个数据校验条件,ValidationRule是抽象类,使用的时候需要创建它的派生类并实现它的Validate方法,Validate方法的返回值是ValidationResult类型对象,如果校验通过,就把ValidationResult对象的IsValid属性设为true,反之,设为false,并为其ErrorContent属性设置一个合法的消息内容

界面代码

        <TextBox x:Name="textBox1" Margin="5"></TextBox>
<Slider x:Name="slider1" Minimum="0" Maximum="100" Margin="5" />

TextBox用于设置滚动条的值,需要对TextBox中的值进行校验,首先需要准备一个校验类

    class RangeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
double d = 0;
if (double.TryParse(value.ToString(), out d))
{
if ((0 <= d) && (d <= 100))
{
return new ValidationResult(true, null);
}
} return new ValidationResult(false, "输入值非法");
}
}

然后在后台代码中建立这样的Binding

            Binding binding = new Binding();
binding.Source = slider1;
binding.Path = new PropertyPath("Value");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
RangeValidationRule rvr = new RangeValidationRule();
binding.ValidationRules.Add(rvr);
textBox1.SetBinding(TextBox.TextProperty, binding);

Binding进行校验时的默认行为是认为来自Source的数据始终是正确的,只有来自Target的数据才有可能有问题,为了不让有问题的数据污染Source所以需要校验,所以,Binding只有在Target被外部方法更新时校验数据,而来自Binding的Source数据更新Target时是不会进行校验的。如果想在Target改变Source数据时也进行校验,就需要将验证条件的ValidatesOnTargetUpdated属性设置为true。

修改前台代码为

    <StackPanel>
<TextBox x:Name="textBox1" Margin="5"></TextBox>
<Slider x:Name="slider1" Minimum="-10" Maximum="10" Margin="5" />
</StackPanel>

绑定增加一行代码

            Binding binding = new Binding();
binding.Source = slider1;
binding.Path = new PropertyPath("Value");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
RangeValidationRule rvr = new RangeValidationRule();
rvr.ValidatesOnTargetUpdated = true;
binding.ValidationRules.Add(rvr);
textBox1.SetBinding(TextBox.TextProperty, binding);

当滑块拖动到负值时,TextBox显示校验失败

校验失败时,ValidationResult对象携带了一条错误消息,想要显示这条消息,首先在创建Binding时要把Binding对象的NotifyOnValidationError属性设置为true

数据绑定(九)Binding的数据校验的更多相关文章

  1. Binding(四):数据校验

    ​    除了上一节讲的类型转换器,Binding还自带数据校验功能,这节主要来讲一下. 跟类型转换器一样,数据校验需要我们继承ValidationRule类,实现其中的Validate方法,并写入我 ...

  2. springMVC数据校验与单文件上传

    spring表单标签:    <fr:from/> 渲染表单元素    <fr:input/>输入框组件    <fr:password/>密码框组件标签    & ...

  3. 使用jsr303实现数据校验

    除了前端的js验证,服务端也可加入数据验证,springmvc中有两种方式可以验证输入 利用spring自带的验证框架 利用jsr303实现 jsr303实现数据校验 jsr303是java为bean ...

  4. Spring MVC @InitBinder 数据绑定 & 数据格式化 & 数据校验

    1 数据绑定 2 数据格式化 修改绑定的字段等等操作 日期 - 接收表单日期字符串格式内容.,在实体类加入@DateTimeFormat 数值 原理: DefautFormattingConversi ...

  5. SpringMVC听课笔记(九:数据转换 & 数据格式化 & 数据校验)

    1.数据绑定流程 --1). Spring MVC主框架将ServletRequest对象及目标方法入参实例传递给WebDataBinderFactory实例,以创建DataBinder实例对象. - ...

  6. Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验

    Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...

  7. WPF 之 Binding 对数据的校验与转换(三)

    一.前言 ​ Binding 的作用就是架在 Source 和 Target 之间的桥梁,数据可以在这座桥梁的帮助下来流通.就像现实中的桥梁会设置一些关卡进行安检一样,Binding 这座桥上也可以设 ...

  8. Struts2学习笔记(九)——数据校验

    Struts2的数据校验属于服务器端校验,Struts2 支持校验方式 : 手动校验(代码校验) :在服务器端通过编写java代码,完成数据校验 自动校验(配置校验) :XML配置校验(主流) 和 注 ...

  9. WPF使用IDataErrorInfo进行数据校验

    这篇博客将介绍如何使用IDataErrorInfo进行数据校验.下面直接看例子.一个Customer类,两个属性(FirstName, Age) class Customer { public str ...

随机推荐

  1. poj 2965 The Pilots Brothers&#39; refrigerator

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18040 ...

  2. window对象属性alert、confirm、prompt怎么使用?

    window对象属性alert.confirm.prompt怎么使用? 一.总结 1.参数依次复杂,返回值依次复杂,但是感觉都是一一继承的,所以很好想也很好写. 二.window对象alert.con ...

  3. [React] Keep Application State in Sync with Browser History

    Using pushState and passing route data via context allows our application to respond to route change ...

  4. tky项目第②个半月总结

    在上一篇半月总结中,介绍了tky项目的整体架构.项目的进展情况.项目的优势与开发中存在的问题等.今天来聊聊这半个月中,项目中发生的事情. 在这半个月中,项目中有了较大的突破:成功通过了国家评測中心的測 ...

  5. 使用Toolbar + DrawerLayout快速实现高大上菜单侧滑

    如果你有在关注一些遵循最新的Material Design设计规范的应用的话(如果没有,假设你有!),也许会发现有很多使用了看起来很舒服.很高大上的侧滑菜单动画效果,示例如下(via 参考2): 今天 ...

  6. Qt Roadmap for 2018(对3D有很多改进)

    When it comes to new features, we have many things ongoing related to graphics, so I’ll start with t ...

  7. JDBC连接数据库中CallableStatement执行有参存储过程及注解其他

    Oracle的建有参存储过程的过程 procedure pro_01(v_01 in number,v_02 out varchar2) as begin select name into v_02 ...

  8. java架构之项目结构(entity / DTO / VO)

    定义类的讲究 关系示例 定义类的讲究 ejb Enterprise JavaBean(EJB),企业javaBean.是java的核心代码,分别是会话Bean(Session Bean).实体Bean ...

  9. _Decoder_Interface_init xxxxxx in amrFileCodec.o

    Undefined symbols for architecture arm64: "_Decoder_Interface_init", referenced from: Deco ...

  10. 【hdu2825】ac自动机 + 状压dp

    传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...