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呢?需要想想看。
查看全文
相关阅读:
HDU 5446 CRT+Lucas+快速乘
awk的用法
【福利】小程序开发资源干货汇总
前端css常用的选择小汇
前端css常用的选择小汇
前端,Java,产品经理,微信小程序,Python等资源合集大放送
前端,Java,产品经理,微信小程序,Python等资源合集大放送
bootstrap+fileinput插件实现可预览上传照片功能
bootstrap+fileinput插件实现可预览上传照片功能
Jquery前端分页插件pagination同步加载和异步加载
原文地址:https://www.cnblogs.com/tansm/p/902996.html
最新文章
[HDOJ1078]FatMouse and Cheese(记忆化搜索)
hdu 1573 X问题 两两可能不互质的中国剩余定理
hdu 2874 Connections between cities 带权lca判是否联通
poj 1986 Distance Queries 带权lca 模版题
hdu 2586 How far away ? 带权lca
poj 3468 A Simple Problem with Integers 线段树加延迟标记
bzoj 1832 lca
hdu 2795 Billboard 线段树+二分
hdu 5666 Segment 俄罗斯乘法或者套大数板子
codevs 1191 数轴染色 区间更新加延迟标记
热门文章
codevs 1082 线段树练习 3 区间更新+延迟标记
HDU 4771 Stealing Harry Potter's Precious dfs+bfs
BNU 13064 Dice (I) 前缀和优化DP
HDU 5489 Difference of Clustering 图论
BNU 13024 . Fi Binary Number 数位dp/fibonacci数列
BNUOJ 13098 约瑟夫环问题
BestCoder Round #60.1003.GT and set/HDU5506 dfs
BestCoder Round #60/HDU 5505 暴力数学
Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀+后缀
Codeforces Round #325 (Div. 2)D. Phillip and Trains BFS
Copyright © 2011-2022 走看看