对 WPF 中的绑定控件进行强制验证
我有一个 WPF 对话框,上面有几个文本框。 文本框绑定到我的业务对象并附加了 WPF 验证规则。
问题是用户可以完美地单击“确定”按钮并关闭对话框,而无需实际将数据输入到文本框中。 验证规则永远不会触发,因为用户甚至没有尝试将信息输入到文本框中。
是否可以强制进行验证检查并确定某些验证规则是否被破坏?
当用户尝试关闭对话框时我可以执行此操作,并在任何验证规则被破坏时禁止他执行此操作。
谢谢。
I have a WPF dialog with a couple of textboxes on it.
Textboxes are bound to my business object and have WPF validation rules attached.
The problem is that user can perfectly click 'OK' button and close the dialog, without actually entering the data into textboxes. Validation rules never fire, since user didn't even attempt entering the information into textboxes.
Is it possible to force validation checks and determine if some validation rules are broken?
I would be able to do it when user tries to close the dialog and prohibit him from doing it if any validation rules are broken.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 3.5SP1 / 3.0SP2 中,他们还在 ValidationRule 基础中添加了一个新属性,即 ValidatesOnTargetUpdated="True"。 这将在源对象绑定后立即调用验证,而不是仅在更新目标控件时调用验证。 这可能并不完全是您想要的,但是最初看到您需要修复的所有内容也不错。
工作原理是这样的:
In 3.5SP1 / 3.0SP2, they also added a new property to the ValidationRule base, namely, ValidatesOnTargetUpdated="True". This will call the validation as soon as the source object is bound, rather than only when the target control is updated. That may not be exactly what you want, but it's not bad to see initially all the stuff you need to fix.
Works something like this:
我们的应用程序中也存在这个问题。 验证仅在绑定更新时触发,因此您必须手动更新它们。 我们在 Window 的 Loaded< 中执行此操作/a> 事件:
这将使错误模板(红色轮廓)出现,并设置 Validation.HasError 属性,我们触发“确定”按钮来禁用该属性:
We have this issue in our application as well. The validation only fires when bindings update, so you have to update them by hand. We do this in the Window's Loaded event:
This will make the error template (red outline) appear, and set the Validation.HasError property, which we have triggering the OK button to disable:
这是一种不需要调用“UpdateSource()”或“UpdateTarget()”的替代方法:
Here is an alternative way that doesn't require calling "UpdateSource()" or "UpdateTarget()":
以防万一有人碰巧发现这个老问题并且正在寻找解决 Monstieur 关于 UI 指南的评论的答案,我执行了以下操作:
Xaml
requiredFieldValidationRule:
在 Xaml 绑定到的类中
,然后使用绑定属性隐藏我的“保存”按钮,如果我的任何对象有 HasValidationError = true。
希望这对某人有帮助。
Just in case anyone happens to find this old question and is looking for an answer that addresses Monstieur's comment about UI guidelines, I did the following:
Xaml
RequiredFieldValidationRule:
In the class that the Xaml binds to
I then hide my Save button using a bound property, if any of my objects has HasValidationError = true.
Hope this is helpful to someone.
使用 Robert Macnee 提出的上述方法。 例如:
但是,在运行此代码之前,请确保绑定的控件是可见的!
Use the method above proposed by Robert Macnee. For example:
But, BE SURE that the bound controls are Visibles before this code run!
在数据对象上使用 INotifyPropertychanged,
您可以将以下代码添加到控件中。
文本框订阅 datacontext 对象(在我们的示例中为 MyObjet)的 propertychanged 事件,并假设在源数据更新时触发它,
它会自动强制刷新到控件
无需调用自己的 UpdateTarget 方法
using the INotifyPropertychanged on your data object
you can add the following code to your control
The textbox susbscribe to the propertychanged event of the datacontext object ( MyObjet in our example) and assumes it is fired when the source data has been updated
it automatically forces the refresh to the control
No need to call yourself the UpdateTarget method