如何在WPF中更改“绑定选项"的默认值? [英] How can i change the default values of the Binding Option in WPF?

查看:351
本文介绍了如何在WPF中更改“绑定选项"的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我正在使用几个文本框控件,其内容由来自数据库的对象填充.该对象使用验证来验证文本的正确插入.

In my current project i'm using several textbox controls whose content is filled from objects which are coming from a database. The object uses validation to validate the correct insertion of the text.

当我想显示验证错误(即文本必须包含多个字符)时,我必须向text属性添加一些绑定选项,如以下行所示:

When i want to show a validation error (i.e. the text has to many characters) i have to add some binding options to the text property like in the following line:

<TextBox Text="{Binding Mode=TwoWay, Path=Description, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" />

是否可以创建模板或样式,或者将后三个选项(ValidatesOnDataErrors,NotifyOnValidationError,UpdateSourceTrigger)的默认值更改为上面代码中的值?文本框控件应如下所示:

Is it possible to create a template or style or whatever to change the default values of the last three options (ValidatesOnDataErrors, NotifyOnValidationError, UpdateSourceTrigger) to the values like in the code above? The Textbox controls should look like the follwing then:

<TextBox Text="{Binding Mode=TwoWay, Path=Description}" />

推荐答案

鉴于这是WPF(不是Silverlight),我认为您确实可以选择:自定义标记扩展.这样的扩展名可以构造并返回Binding,但是您会喜欢它,并且将导致一个简单的用法模式,例如:

Given that this is WPF (not Silverlight) I think you do have an option: a custom markup extension. Such an extension could construct and return a Binding however you like, and would result in a simple usage pattern such as:

<TextBox Text="{ValidatedBinding Description}"/>

有关实现自定义标记扩展的详细信息,请参见此处.

For more information on implementing a custom markup extension, see here.

这篇关于如何在WPF中更改“绑定选项"的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆