这篇博客将介绍如何使用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判断浏览器类型以及浏览器版本

    判断浏览器类型:   if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAg ...

  2. bzoj2091【Poi2010】The Minima Game

    直接dp就好了 每个人肯定会去选最大的,用dp[i]表示选了后i个点时先手-后手的最大值(因为从后往前扫才好转移啊 QwQ~) dp[i]=max(c[j]-dp[j-1]),(j<=i) 直接 ...

  3. Codeforces Round #373 (Div. 2)

    A,B,C傻逼题,就不说了. E题: #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  4. hdu5000 背包dp

    题意可抽象为:N个包中每个包容量是T[i],每个包都拿一些,设拿出的总数为sum时的方案数为q,求max(q) 设dp[i][j]为拿了前i个包,共拿出了j物品时的方案数.那么 for i=1 to ...

  5. RabbitMQ消息队列(一): Detailed Introduction 详细介绍

     http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...

  6. SourceTree推送时,增加额外的远程仓库,不用每次都自定义粘贴复制网络

    一.命令行添加 二.软件界面可以查看到结果 更新,以上是代码添加了远程仓库,最近,找到了不用代码,直接在文件夹里写地址来添加的方式.直接上图了.

  7. hibernate的集中持久化方法的区别

    一.预备知识 在所有之前,说明一下,对于hibernate,它的对象有三种状态,transient.persistent.detached 下边是常见的翻译办法: transient:瞬态或者自由态 ...

  8. git gui 学习

    目的 自己以前使用过3,4个月的SVN,因为公司使用的是git,git gui.所以打算自学git gui,并记录一下学习心得.^_^ 原因 为什么不是学命令行而是用git gui呢.我觉得首先因为公 ...

  9. JS之Form表单相关操作

    获取ID组件的值 var userid=document.getElementById('userid').value;var cdkey=document.getElementById('cdkey ...

  10. linux零基础入门总结

    啊,notepad++贴过来怎么对齐格式这么糟糕呢? #root用户 $普通用户   linux命令 清屏clear翻页清屏  CRT中ctrl+L    reset"清空 CRT中不起作用 ...