在向用户呈现验证错误之前创建一个延迟 [英] Create a delay before validation errors are presented to the user
问题描述
在遵循MVVM模式的特定WPF应用程序中,视图模型实现IDataErrorInfo接口,以在文本字段中通知无效数据视图。
In this specific WPF application which follows the MVVM pattern, the view model implements the IDataErrorInfo interface to notify the view of invalid data in text fields.
文本框存在于您可以输入体积的视图中。这是通过属性更改的更新源指定的,并验证数据错误:
A text box exists in the view where you can enter a volume. This has been specified with property changed update source, and validate data errors:
<TextBox
Text="{Binding Volume, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
此问题是您在用户完成输入之前收到验证错误。例如,有效值为 25 ml。但是在用户键入最后一个 l之前,文本框中将出现 25 m。这不是有效值,将导致IDataError实现这样说。
The problem with this is that you get a validation error before the user has finished typing. E.g., a valid value is "25 ml". But before the user has typed in the last "l", then "25 m" is present in the text box. This is not a valid value and will result in the IDataError implementation saying so.
结果是,随着用户的输入,红色标记在文本框周围闪烁。
The result is, as the user is typing, a red marker flashes around the text box.
我们希望在文本框周围的红色标记出现之前有一点延迟(0.5秒),因此我们可以假设用户在完成验证之前已经完成了键入操作错误。
We would like to have a little delay (0.5 sec) before the red marker appears around the text box, so we can assume that the user has finished typing before showing validation errors.
第一次尝试是创建一个专用的文本框,该框等待0.5秒后再更新视图模型中的属性。但这不好,因为在用户确实输入有效值的情况下,则在启用提交按钮之前0.5秒过去了。
The first attempt was to create a specialized text box that waits .5 sec before updating the property in the view model. But that is no good, because in the case that the user does enter a valid value, then .5 second passes before the submit button gets enabled.
我有个主意您可以编写实现此行为的专用绑定(即创建派生自System.Windows.Data.Binding的专用类),但我不知道如何执行此操作。
I have an idea that you could write a specialized binding (i.e. create a specialized class derived from System.Windows.Data.Binding) that implements this behavior, but I have no idea how to do this.
这是一个合理的方法,还是有更好的方法?
Is that a plausible way, or is there a better?
推荐答案
听起来像您可以使用Paul的自定义DelayBinding Stovell发表了关于。我成功实施了延迟搜索/过滤功能,并成功使用了它。您可以在此处阅读以下内容:
Sounds like you could use a custom DelayBinding that Paul Stovell blogged about. I've used it with great success implementing delayed search/filtering. You can read about it here:
http:/ /www.paulstovell.com/wpf-delaybinding
这篇关于在向用户呈现验证错误之前创建一个延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!