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呢?需要想想看。
查看全文
相关阅读:
Function to use in Queries, Filters
Dynamics AX 2012
Job to Import Vendor/Customer Postal Address in Dynamics Ax2012
To Find or Update customer primary Address in Ax 2012
Importing Customers, Vendors and Products in AX 2012
CobaltStrike安装教程
kali安装dnsdict6
查看windows端口被占用
使用reGeorg打穿HTTP隧道代理
windows建立隐藏用户
原文地址:https://www.cnblogs.com/tansm/p/902996.html
最新文章
JAVA高级复习-线程的优先级
JAVA高级复习-线程中常用的方法
JAVA高级复习-多线程创建方式一(匿名子类的方式调用线程中的run()方法)
JAVA高级复习-多线程创建方式一
JAVA基础复习-接口匿名实现类的对象
restful风格url Get请求查询所有和根据id查询的合并成一个controller
localStorage前端存储数据
IN语句改写EXISTS
springboot整合swagger
面试题相关
热门文章
idea必选配置
随机生成6位字母+数字混合密码
获取项目的根目录路径
mybatis逆向工程
文件下载时返回的文件名乱码的问题
Send email from dynamics ax
AX2012 查询财务维度
ax 2012 write to file
Copy from common to common
Formatting Numbers [SSRS]
Copyright © 2011-2022 走看看