zoukankan
html css js c++ java
WPF学习笔记(数据绑定篇3)
接上回的《
WPF学习笔记(数据绑定篇2)
》,继续
BindValidation
此示例演示了:
如何使用错误模板;
使用样式显示错误信息;
如何在校验发生异常时执行回调;
首先,你可以看见XAML中使用自定义的错误模板,指定错误模板的方式是:
<
TextBox
Name
="textBox1"
Validation.ErrorTemplate
="{StaticResource validationTemplate}"
Style
="{StaticResource textBoxInError}"
>
<
TextBox
.Text
>
此错误模板我简单改造了一下,变得好看点:
<
ControlTemplate
x:Key
="validationTemplate"
>
<
DockPanel
>
<
Image
Source
="Error.jpg"
Width
="16"
Height
="16"
/>
<
AdornedElementPlaceholder
/>
</
DockPanel
>
</
ControlTemplate
>
注意这个模板是ControlTemplate(为什么是控件模板我也不知道,照葫芦画瓢),然后定义了一个布局,左边一个图像,右边一个AdornedElementPlaceholder占位符。
(我在实验时,图像如果没有加入Width和Height,在显示时图片将变得很大)。
当然,你也可以使用样式绑定到异常上来显示错误,例如:
<
Style
x:Key
="textBoxInError"
TargetType
="{x:Type TextBox}"
>
<
Style
.Triggers
>
<
Trigger
Property
="Validation.HasError"
Value
="true"
>
<
Setter
Property
="ToolTip"
Value
="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"
/>
</
Trigger
>
</
Style.Triggers
>
</
Style
>
当然,例子中还显示了,如果校验时发生异常(注意:是异常不是不正确的数据),将发生回调:
BindingExpression myBindingExpression
=
textBox3.GetBindingExpression(TextBox.TextProperty);
Binding myBinding
=
myBindingExpression.ParentBinding;
myBinding.UpdateSourceExceptionFilter
=
new
UpdateSourceExceptionFilterCallback(ReturnExceptionHandler);
myBindingExpression.UpdateSource();
当然,为什么是ParentBinding呢?需要想想看。
查看全文
相关阅读:
JVM学习笔记之认识JDK(一)
C#发送邮件异常:根据验证过程,远程证书无效
windows下使用mysql双机热备功能
批处理实现mysql的备份
WebApi FormData+文件长传 异步+同步实现
Oracle中已知字段名查询所在的表名
mstsc遇到CredSSP加密Oracle修正
使用subgit进行svn迁移至git(branch,tags)
使用guava进行对字符串的加锁
使用spring-data-solr做solr客户端
原文地址:https://www.cnblogs.com/tansm/p/902996.html
最新文章
Flink入门
Flink入门
SOFABoot&SOFATracer
Flink原理、实战与性能优化读书笔记
Code Clean读书笔记
蚂蚁金服-架构演进
SQL实现交,并,差操作
Spring-事务管理(Transaction)
Kotlin教程——史上最全面、最详细的学习教程,持续更新中....
Kotlin——中级篇(二): 属性与字段详解
热门文章
Kotlin——高级篇(五):集合之常用操作符汇总
Kotlin——高级篇(四):集合(Array、List、Set、Map)基础
Kotlin——初级篇(八):关于字符串(String)常用操作汇总
Kotlin——高级篇(二):高阶函数详解与标准的高阶函数使用
Kotlin——中级篇(四):继承类详解
Kotlin——高级篇(一):Lambda表达式详解
Kotlin——初级篇(七):函数(方法)基础总结
Markdown语法你都会了吗?
JVM学习笔记之初识JVM(三)
JVM学习笔记之JDK、JRE、JVM的关系(二)
Copyright © 2011-2022 走看看