WPF 中的 MVVM 和转换错误 - 将视图中的 ConvertBack 异常与模型中的验证错误相结合?

发布于 2024-10-30 13:06:26 字数 842 浏览 6 评论 0原文

我正在使用 Josh Smith 在 文章中描述的 MVVM 模式开发 WPF 应用程序http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

我无法找出对转换回错误做出反应的好方法(例如,当用户在绑定到双精度值的文本框中键入字母时)。

Josh Smith 提出了一个验证系统,其中模型验证其自身的值并通过 SomeModel.IsValid 呈现布尔结果。然后,Josh 使用 IsValid 的值作为某些按钮的谓词,例如表单上的“保存”按钮 - 如果表单尚未有效完成,则 IsValid 为 false 并且“保存”按钮被禁用。

这个方法确实很有效。但是,当用户在文本框中键入无法转换的值时,会发生 ConvertBack 错误。 ViewModel 的属性 setter 永远不会被调用,因此 Model 的属性永远不会更新。 IsValid 仍然为 true。尽管视图通过显示错误消息并突出显示错误(或任何设置)来响应验证错误,但“保存”按钮仍保持活动状态,因为模型的 IsValid 属性仍然为 true,因为模型尚未更新。

有没有 MVVM 用户经历过同样的挑战?有什么想法吗?

一种建议是模型上的属性可以为 Nullable。然后,如果用户输入不可转换的日期(例如绑定到双精度数的文本框中的字母),则转换器应将源设置为 Null。但是,我找不到一种简单的方法来做到这一点 - 似乎需要为我需要功能的每种数据类型以及我需要它的每种数字格式编写自定义转换器。对于看起来像这样的问题,这是一个非常糟糕的解决方案设计挑战。

有什么想法吗?

I am developing a WPF application using the MVVM pattern as described by Josh Smith in the article at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx.

I can't figure out a good way of reacting to Convert Back errors (for example when a user types a letter in a textbox bound to a double).

Josh Smith proposes a Validation system where the Model validates it's own values and presents the boolean result through SomeModel.IsValid. Josh then uses the value of IsValid as predicate for certain buttons, e.g. a Save button on a form - if the form has not been validly completed, IsValid is false and the Save button is disabled.

This method works really well. However, when a user types a value in a textbox that cannot be converted, a ConvertBack error occurs. The ViewModel's setter for the property is never called, and thus the Model's property is never updated. IsValid remains true. Although the View responds to the Validation error by showing an error message and highlighting the error (or whatever the setting may be), the Save button remains active, as the IsValid property of the Model is still true, as the Model has not been updated.

Are there any MVVM users who have experienced this same challenge? Any ideas?

One suggestion is that the property on the model could be Nullable. Then the Converter should set the source to Null if the user enters non-convertable date (such as a letter in a textbox bound to a double). However, I could not find a simple way of doing this - it seems to require writing custom converters for every data type I require the functionality for, and for every number format I require it in. This is a very poor sollution to what seems like a design challange.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2024-11-06 13:06:26

我发现了两篇关于这个问题的优秀文章:

Karl Shifflett 的一篇:
http://karlshifflett.wordpress.com/mvvm/ input-validation-ui-exceptions-model-validation-errors/

Josh Smith 的一篇:

http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error -messages/

这有点太复杂了,无法在此处复制,因此我将链接标记为答案。

I have found two excellent articles addressing this issue:

One by Karl Shifflett:
http://karlshifflett.wordpress.com/mvvm/input-validation-ui-exceptions-model-validation-errors/

One by Josh Smith:

http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages/

It's a bit too complex to duplicate here, so I will just mark the links as answer.

方圜几里 2024-11-06 13:06:26

您可能对 WPF 应用程序框架 (WAF)BookLibrary 示例应用程序感兴趣a>。它显示了如何将 Parsing 或 ConvertBack 异常作为验证错误进行处理。

只需在“页面”文本框中输入“abc”,然后查看验证错误如何显示为工具提示以及“保存”按钮如何被禁用。

You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It shows how Parsing or ConvertBack exceptions are handled as validation errors.

Just enter "abc" in the "Pages" TextBox and see how the validation error is shown as tooltip and the Save button is disabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文