WPF使用IDataErrorInfo进行数据校验
这篇博客将介绍如何使用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进行数据校验的更多相关文章
- wpf企业应用之数据校验
wpf中使用IDataErrorInfo实现数据校验,绑定实体需要实现了此接口,并在UI绑定表达式中添加ValidatesOnDataErrors=True,这样数据校验发生时,wpf会调用该接口中的 ...
- WPF使用IDataErrorInfo接口进行数据校验 - 简书
原文:WPF使用IDataErrorInfo接口进行数据校验 - 简书 class ValidationBindableBase : BindableBase, IDataErrorInfo { pu ...
- [WPF 基础知识系列] —— 绑定中的数据校验Vaildation
前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...
- [转]深入浅出WPF(7)——数据的绿色通道,Binding
本文转自:http://liutiemeng.blog.51cto.com/120361/95273 小序: 怎么直接从2蹦到7啦?!啊哦,实在是不好意思,最近实在是太忙了,忙的原因也非常简单——自己 ...
- SilverlightMVVM模式中的数据校验
silverlight的数据校验大体分成3种类型: 数据是非必填的但是需要满足相应数据格式的 数据是必填的且可能需要进行数据格式校验的 其他(如数据的联动校验) 以下的数据校验方式针对第二种: 在相应 ...
- [C#.NET 拾遗补漏]09:数据标注与数据校验
数据标注(Data Annotation)是类或类成员添加上下文信息的一种方式,在 C# 通常用特性(Attribute)类来描述.它的用途主要可以分为下面这三类: 验证 Validation:向数据 ...
- Struts2数据校验
Struts2数据校验 1.常见数据校验方法 表单数据的校验方式: 表单中的数据必须被效验以后才能够被使用,常用的效验方式分为两种: 前台校验:也称之为客户端效验,主要是通过JS编程的方式进行表单数据 ...
- Spring MVC数据校验
在web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对 数据进行验证.输入验证分为客户端验证与服务器端验证.客户端验证主要通过JavaScript脚本进行,而服务器端验证则主要通过Jav ...
- 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.访问页 ...
随机推荐
- RabbitMQ headers Exchange
Headers Exchange headers也是一种交换机类型,但是在rabbitmq官网中的教程中并没有说到.资料也很少,但是找一找总会有的. headers与direct的模式不同,不是使用r ...
- yii2 composer安装
安装Yii2 1.安装composer 在命令行输入 curl-sS https://getcomposer.org/installer | php mv composer.phar /usr/loc ...
- linux 安装 ArcSDE10.1
实验仍未成功,步骤仅供参考. 1:首先检查一下在Linux操作系统下Oracle数据库是否能启动,是否能连通等 [oracle@localhost ~]$ sqlplus SQL*Plus: Rele ...
- ANT的安装
1.下载ANT http://ant.apache.org/bindownload.cgi 2.将下载下来的压缩包解压到任意文件夹下,例如D盘根目录下D:/apache-ant-1.9.2 3.添加环 ...
- 编译安装 Zend Opcache 缓存Opcache,加速 PHP
Optimizer+ 是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件,是第一个也是最快的 opcode 缓存工具.现在,Zend 科技公司将 Optimizer+ 在 PHP Lice ...
- linux安装php & nginx
1.安装libxml2 地址:http://ftp.gnome.org/pub/GNOME/sources/libxml2/ wget http://caesar.acc.umu.se/pub/GNO ...
- 分享一个Visual Studio的背景插件,让堆码更富情趣
忘记一件重要的事情,我使用的是VS 2012版,其他更高版本应该是可以找到的,以下版本就不清楚了.有可能找不到,见谅,也不是我开发的,只是偶尔碰到,拿出来让大家知道. 上周某日,新生命群里面还是一如既 ...
- div内填内容
<div contenteditable="true">可以编辑里面的内容</div> 这样就可以使div想textarea一样 可以加入自动换行与over ...
- soundtouch变速wsola算法之改进
soundtouch变速算法很类似sola算法,细看才知道是wsola算法. 上个星期有个需求,将该变速应用到直播的包处理,有点类似于webrtc的netEQ处理机制. 直接使用soundtouch, ...
- DNS解析过程和域名收敛、域名发散、SPDY应用
前段时间项目要做域名收敛,糊里糊涂的完成了,好多原理不清晰,现在整理搜集下知识点. 域名收敛的目的是什么?简单来说就是域名解析慢.那为什么解析慢?且听下文慢慢道来. 什么是DNS? DNS( Doma ...