这篇博客将介绍如何使用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. js获取域名

    <script language="javascript">//获取域名host = window.location.host;host2=document.domai ...

  2. 微信连WiFi expired timestamp 和sign错误小坑解决

    0.微信连WiFi需要时间戳毫秒,但是PHP本身没有自带这个函数.但是相对来说,Java和js获取毫秒时间戳就比较方便. 既然PHP没有,那么就自己写一个获取毫秒时间戳的函数,否则就会失败.实在懒得写 ...

  3. Python 面向对象 中高级

    类成员: #字段 普通字段 属于对象 执行只能通过对象访问 静态字段 属于类 执行 既可以通过对象访问,也可以通过类访问 class Foo: def __init__(self,name): # 字 ...

  4. 学习笔记——git

    恩没错,又是个新东西 使用Git提交文件到版本库有两步: 第一步:是使用 git add 把文件添加进去,实际上就是把文件添加到暂存区. 第二步:使用git commit提交更改,实际上就是把暂存区的 ...

  5. 【CityHunter】服务器端设计思路

    设计服务端程序首先我考虑到的是通讯传输方式的设计,按照CityHunter的特殊性,其具有两种使用场景: 仅用于查看当前信息状态.搜索周边环境.对信息的实时性要求不高的一些场景: 用于攻略藏宝图或Ch ...

  6. 创建一个点状注记(MarkerElement)

    1.根据XY创建一个点 /// <summary> /// 根据x y创建新点 /// </summary> /// <param name="dX" ...

  7. Unity unsafe

    http://answers.unity3d.com/questions/23307/is-it-impossible-to-use-the-code-with-keyword-unsa.html 可 ...

  8. Android 签名证书

    Android APK的数字签名的作用和意义 http://blog.csdn.net/gaomatrix/article/details/6568191 http://jingyan.baidu.c ...

  9. C++ 11 中的右值引用

    C++ 11 中的右值引用 右值引用的功能 首先,我并不介绍什么是右值引用,而是以一个例子里来介绍一下右值引用的功能: #include <iostream>    #include &l ...

  10. ueditor 百度编辑器,取消或自定义右键菜单

    如图:有2种自定义方法,一种是改源码,一种是初始化 初始化,如下代码: var ue = UE.getEditor('XXXid',{ // contextMenu:[ {label:'', cmdN ...