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

class Customer
{
public string FirstName
{
get;
set;
} public int Age
{
get;
set;
}
}

将Customer类继承IDataErrorInfo,并实现它的属性。

    class Customer : System.ComponentModel.IDataErrorInfo
{
public string this[string columnName]
{
get
{
string result = string.Empty; if(columnName == "FirstName")
{
if(string.IsNullOrWhiteSpace(FirstName))
{
result = "Name cannot null or empty.";
}
}
else if(columnName == "Age")
{
if(Age < )
{
result = "Age cannot less then zero.";
}
} return result;
}
}
public string Error
{
get
{
return null;
}
} public string FirstName
{
get;
set;
} public int Age
{
get;
set;
}
}

在UI中绑定Customer的FirstName,Age属性,并且当出现错误数据时触发验证。

    <Window.Resources>
<local:Customer x:Key="CustomerInstance" FirstName="Sam Bent" Age="" />
<ControlTemplate x:Key="TextBoxErrorTemplate">
<Grid>
<Border BorderBrush="Blue" BorderThickness="">
<AdornedElementPlaceholder/>
</Border>
</Grid>
</ControlTemplate>
</Window.Resources>
<StackPanel Margin="">
<TextBox
Text="{Binding
Source={StaticResource CustomerInstance},
Path=FirstName,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"
Validation.ErrorTemplate="{x:Null}"

Margin="0,5" /> <TextBox Text="{Binding
Source={StaticResource CustomerInstance},
Path=Age,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}"
Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>

</TextBox>
</StackPanel>

将Customer的FirstName与Age属性分别绑定在两个TextBox中,设置ValidatesOnDataErrors=True来触发验证。将错误信息绑定在TextBox的ToolTip属性上,

ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" 或者

ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"或者

<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>

另外可以对ErrorTemplate进行定制,例如上面代码中的TextBoxErrorTemplate。

运行结果:

代码点击这里下载,感谢您的阅读。

WPF使用IDataErrorInfo进行数据校验的更多相关文章

  1. wpf企业应用之数据校验

    wpf中使用IDataErrorInfo实现数据校验,绑定实体需要实现了此接口,并在UI绑定表达式中添加ValidatesOnDataErrors=True,这样数据校验发生时,wpf会调用该接口中的 ...

  2. WPF使用IDataErrorInfo接口进行数据校验 - 简书

    原文:WPF使用IDataErrorInfo接口进行数据校验 - 简书 class ValidationBindableBase : BindableBase, IDataErrorInfo { pu ...

  3. [WPF 基础知识系列] —— 绑定中的数据校验Vaildation

    前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...

  4. [转]深入浅出WPF(7)——数据的绿色通道,Binding

    本文转自:http://liutiemeng.blog.51cto.com/120361/95273 小序: 怎么直接从2蹦到7啦?!啊哦,实在是不好意思,最近实在是太忙了,忙的原因也非常简单——自己 ...

  5. SilverlightMVVM模式中的数据校验

    silverlight的数据校验大体分成3种类型: 数据是非必填的但是需要满足相应数据格式的 数据是必填的且可能需要进行数据格式校验的 其他(如数据的联动校验) 以下的数据校验方式针对第二种: 在相应 ...

  6. [C#.NET 拾遗补漏]09:数据标注与数据校验

    数据标注(Data Annotation)是类或类成员添加上下文信息的一种方式,在 C# 通常用特性(Attribute)类来描述.它的用途主要可以分为下面这三类: 验证 Validation:向数据 ...

  7. Struts2数据校验

    Struts2数据校验 1.常见数据校验方法 表单数据的校验方式: 表单中的数据必须被效验以后才能够被使用,常用的效验方式分为两种: 前台校验:也称之为客户端效验,主要是通过JS编程的方式进行表单数据 ...

  8. Spring MVC数据校验

    在web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对 数据进行验证.输入验证分为客户端验证与服务器端验证.客户端验证主要通过JavaScript脚本进行,而服务器端验证则主要通过Jav ...

  9. spring mvc 数据校验

    1.需要导入的jar包: slf4j-api-1.7.21.jar validation-api-1.0.0.GA.jar hibernate-validator-4.0.1.GA.jar 2.访问页 ...

随机推荐

  1. ubuntu/mint 安装google的拼音输入法

    sudo apt-get install fcitx sudo apt-get install fcitx-googlepinyin im-config 即可完成google的输入法 重启计算机.在右 ...

  2. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  3. gulp-rev同时将js和css文件写在一个rev-manifest.json文件里面的方式探讨

    参考: https://segmentfault.com/q/1010000002876613 https://github.com/sindresorhus/gulp-rev 测试发现,在官网上最主 ...

  4. java -日期处理

    1. 计算某年某月份 总有多少个周,每周的开始和结束时间? 思路:1.计算出本月实际的总天数 2.循环每一天,判断这天是否是 周日(1),如果是,周数加1,再次判断是否是月的第一个周一,如是,开始时间 ...

  5. 每日学习笔记:js中可以直接用id名调用的问题?

    在JavaScript中,标准的id选择器调用语法是: document.getElementById('myid').style.width = pc + "%"; 但是,今天发 ...

  6. 在MySQL向表中插入中文时,出现:incorrect string value 错误

    在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8.      一.修改数据库的默认字符集   ...

  7. 入门: 使用JNI 从C++代码中调用Java的静态方法

    开发环境: 操作系统: (uname -a output)  Linux ubuntu 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC ...

  8. hadoop集群安装故障解决

    nodemanager进程解决:http://blog.csdn.net/baiyangfu_love/article/details/13504849 编译安装:http://blog.csdn.n ...

  9. PHP图像裁剪为任意大小的图像,图像不变形,不留下空白

    <?php /** * 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高 */ fu ...

  10. OS X EI Capitan安装refind时出现Could not set boot device property: 0xe00002bc

    参考:terminal - OSX 10.11 El Capitan - setting boot device property not working ... 解决办法: 1.重启MacMini, ...