什么是IDataErrorInfo,它如何与WPF配合使用? [英] What is IDataErrorInfo and how does it work with WPF?

查看:93
本文介绍了什么是IDataErrorInfo,它如何与WPF配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作WPF中的一些自定义验证器时,我的同事之一指出了IDataErrorInfo。我在XAML中有一个示例视图,它具有一个文本框和一个按钮。根据文本框中的值,我希望按钮被启用或禁用。我的同事建议在我的视图的演示者中扩展IDataErrorInfo,并为'Item'和'Error'属性编写自定义逻辑将会解决我的问题。在我可以将其纳入我的代码之前,我以为我应该了解IDataError信息的工作原理,以及它是如何实现这个接口来提供必要的钩子来触发自定义验证逻辑的?一些帮助这个概念将是非常有用的!

解决方案

IDataErrorInfo 是类可以实现的界面,以便通知订阅者特定属性的错误信息,以及在类级别的错误。



如果您为用作DataContext的类实现了此功能(即:MVVM中的ViewModel),则可以将UpdatesOnValidationError设置为true ,并设置自定义模板以在出现错误时以不同的方式显示该项目。 WPF处理您的管道。



这是一个简短教程显示整个过程


While working on some custom validators in WPF, one of my co-workers pointed me out the IDataErrorInfo. I have a sample view in XAML that has a textbox and a button. Based on the value in the textbox I would like the button to be either enabled or disabled. My co-worker suggested that extending the IDataErrorInfo in the presentor of my view and writing custom logic for the 'Item' and 'Error' properties would solve my problem. Before I could incorporate this in my code, I thought I should understand how IDataError info works and what is it about implementing this interface that provides the necessary hooks to trigger the custom validation logic? Some help with this concept would be extremely helpful!

解决方案

IDataErrorInfo is an interface that a class can implement in order to notify subscribers of error information for a specific property, as well as errors at the class level.

If you implement this for the class that's used as your DataContext (ie: the ViewModel in MVVM), you can set UpdatesOnValidationError to true for controls, and set a custom template to display the item differently if there are errors. WPF handles the plumbing for you.

Here is a short tutorial showing the entire process.

这篇关于什么是IDataErrorInfo,它如何与WPF配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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