WPF 验证中的 ValidatesOnNotifyDataErrors 和 ValidatesOnDataErrors 和 NotifyOnValidationError 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17254847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between ValidatesOnNotifyDataErrors and ValidatesOnDataErrors and NotifyOnValidationError in WPF validation?
提问by kuhajeyan
In WPF validation, whats the difference between the following:
在WPF 验证中,以下有什么区别:
ValidatesOnNotifyDataErrors = True
ValidatesOnDataErrors = True
NotifyOnValidationError = True
ValidatesOnNotifyDataErrors = True
ValidatesOnDataErrors = True
NotifyOnValidationError = True
When should you use these properties correctly in XAML?
什么时候应该在 XAML 中正确使用这些属性?
回答by Ibrahim Najjar
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:
ValidatesOnNotifyDataErrors
并且ValidatesOnDataErrors
当您希望 XAML 绑定控件基于在 ViewModel/Model 中实现的接口验证其输入时使用,因为ValidatesOnNotifyDataErrors
该接口是INotifyDataErrorInfo
并且ValidatesOnDataErrors
它是IDataErrorInfo
。
例如,假设您有一个这样的视图模型:
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;
}
}
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.
然后在您的视图中,您有一个绑定到 FirstName 属性的文本框,如下所示:
<TextBox Text={Binding Path=FirstName, ValidatesOnDataErrors=True} />
现在,如果用户在文本框中输入 20 个或更多字符,将检测到错误。
On the other hand NotifyOnValidationError
is used when you want an event to be raised when the bound fails validation.
另一方面NotifyOnValidationError
,当您希望在绑定验证失败时引发事件时使用。
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.
我通常ValidatesOnDataErrors
在我的 XAML 控件中使用进行验证,而我不需要其他两个,所以这取决于您的情况。
EDIT:I am updating my answer as I have learned some new things, so I need to make this more relevant.
编辑:我正在更新我的答案,因为我学到了一些新东西,所以我需要让它更相关。
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
.
ValidatesOnDataErrors
用于胖客户端,或者换句话说,当在客户端执行验证时,例如桌面 WPF 或 WinForm 应用程序和模型对象实现IDataErrorInfo
。
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.
另一方面,ValidatesOnNotifyDataErrors
它更适合瘦客户端(多层应用程序),例如客户端-服务器应用程序(Silverlight、WPF 和 WCF 等),其中验证在服务器上进行。
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 (ErrorsChangedevent 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
.
这样,当用户在 TextBox 中键入某些内容时,该值将异步发送到服务器进行验证,当验证结果返回时,将引发一个事件(准确地说是ErrorsChanged事件),然后视图将其选中并显示它使用适当的方法,当然在这种情况下模型将实现INotifyDataErrorInfo
。
回答by thomasgalliker
Just for your info: IDataErrorInfo.Error
is not used in WPF and can return null or throw a NotImplementedException
.
This property was used in WinForms.
仅供参考:IDataErrorInfo.Error
WPF 中未使用,可以返回 null 或抛出NotImplementedException
.
WinForms 中使用了此属性。
Personally, I prefer to use INotifyDataErrorInfo
because it allows to have multiple error messages mapped to a single property.
就个人而言,我更喜欢使用INotifyDataErrorInfo
它,因为它允许将多个错误消息映射到单个属性。