什么是IDataErrorInfo,它是如何与WPF的工作? [英] What is IDataErrorInfo and how does it work with WPF?

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

问题描述

虽然工作在WPF一些自定义的验证,我的同事之一,指着我对IDataErrorInfo。我在XAML的样本认为,有一个文本框和一个按钮。根据在文本框中的值,我想按钮被启用或禁用。我的同事建议,在我看来,presentor延长对IDataErrorInfo和编写自定义逻辑的'项目'和'错误'的属性就可以解决我的问题。还没等我在我的code结合这一点,我想我应该知道如何IDataError信息有用,哪些是关于实现此接口,提供了必要的挂钩,以触发自定义验证逻辑?一些帮助,这个概念是非常有帮助的!

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 是一个类可以为了在类级别通知出错信息的用户特定属性,以及错误实现一个接口。

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.

如果您实现此为所使用的作为你的DataContext(即:视图模型中MVVM)类,你可以设置UpdatesOnValidationError为true的控件,并设置自定义模板,以不同的方式显示项目,如果有错误。 WPF处理管道给你。

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.

下面是一个简短教程显示整个过程

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

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