WPF 验证中的 ValidatesOnNotifyDataErrors 和 ValidatesOnDataErrors 和 NotifyOnValidationError 有什么区别? [英] What is the difference between ValidatesOnNotifyDataErrors and ValidatesOnDataErrors and NotifyOnValidationError in WPF validation?

查看:126
本文介绍了WPF 验证中的 ValidatesOnNotifyDataErrors 和 ValidatesOnDataErrors 和 NotifyOnValidationError 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF验证中,以下有什么区别:

In WPF validation, whats the difference between the following:

ValidatesOnNotifyDataErrors = True

ValidatesOnNotifyDataErrors = True

ValidatesOnDataErrors = True

ValidatesOnDataErrors = True

NotifyOnValidationError = True

NotifyOnValidationError = True

什么时候应该在 XAML 中正确使用这些属性?

When should you use these properties correctly in XAML?

推荐答案

ValidatesOnNotifyDataErrorsValidatesOnDataErrors 用于当您希望 XAML 绑定控件基于在 ViewModel/Model 中实现的接口,对于 ValidatesOnNotifyDataErrors,该接口是 INotifyDataErrorInfoValidatesOnDataErrorsIDataErrorInfo.
例如,假设您有一个这样的视图模型:

ValidatesOnNotifyDataErrors and ValidatesOnDataErrors are used when you want a XAML bound control to validate its input based on an interface implemented in the ViewModel/Model, for ValidatesOnNotifyDataErrors that interface is INotifyDataErrorInfo and for ValidatesOnDataErrors it is IDataErrorInfo.
for example let's say you have a view model like this:

class PersonViewModel : IDataErrorInfo {

    public string FirstName {get; set;}

    string IDataErrorInfo.Error 
    {
        return string.Empty;
    }

    string IDataErrorInfo.this[string columnName] {
        if (columnName == "FirstName" &&) {
            if (this.FirstName.Length > 20)
                return "FirstName can't be more than 20 characters.";

        }
        return string.Empty;
    }

}

然后在您的视图中,您有一个绑定到 FirstName 属性的文本框,如下所示:<TextBox Text={Binding Path=FirstName, ValidatesOnDataErrors=True}/>现在,如果用户在文本框中输入 20 个或更多字符,则会检测到错误.

and then in your view you have a textbox that is bound to the FirstName property like this: <TextBox Text={Binding Path=FirstName, ValidatesOnDataErrors=True} /> now if the user entered 20 characters or more in the textbox an error will be detected.

另一方面,NotifyOnValidationError 用于当您希望在绑定验证失败时引发事件.

On the other hand NotifyOnValidationError is used when you want an event to be raised when the bound fails validation.

我通常在 XAML 控件中使用 ValidatesOnDataErrors 进行验证,而我不需要其他两个,所以这取决于您的情况.

I usually use ValidatesOnDataErrors in my XAML controls for validation and i haven't had a need for the other two, so it depends on your situation.

我正在更新我的答案,因为我学到了一些新东西,所以我需要让它更相关.

I am updating my answer as I have learned some new things, so I need to make this more relevant.

ValidatesOnDataErrors 用于胖客户端,或者换句话说,当在客户端执行验证时,例如桌面 WPF 或 WinForm 应用程序和模型对象实现 IDataErrorInfo.

ValidatesOnDataErrors is used in thick clients, or in other words when the validation is performed on the client side such as a desktop WPF or WinForm application and model objects implement IDataErrorInfo.

另一方面,ValidatesOnNotifyDataErrors 更适合瘦客户端(多层应用程序),例如客户端-服务器应用程序(Silverlight、WPF使用 WCF 等 ..) 在服务器上进行验证.

On the other hand, ValidatesOnNotifyDataErrors would be a better fit for thin clients (multi-tier applications) such as client-server applications (Silverlight, WPF with WCF, etc ..) where the validation takes place on the server.

通过这种方式,当用户在 TextBox 中键入某些内容时,该值会异步发送到服务器进行验证,当验证结果返回时,将引发一个事件 (ErrorsChanged 事件准确),然后视图选择它并使用适当的方法显示它,当然在这种情况下模型将实现 INotifyDataErrorInfo.

This way when the user types something for example in a TextBox, the value is sent to the server asynchronously for validation, and when validation results come back an event is raised (ErrorsChanged event to be exact), then the view picks that up and displays it using the appropriate method, of course in this case the model would implement INotifyDataErrorInfo.

这篇关于WPF 验证中的 ValidatesOnNotifyDataErrors 和 ValidatesOnDataErrors 和 NotifyOnValidationError 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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