.net core 关于IDataErrorInfo接口的几个问题
我已经介绍了几个实现.net core 关于IDataErrorInfo接口的几个问题,.net-core,idataerrorinfo,.net Core,Idataerrorinfo,我已经介绍了几个实现IDataErrorInfo接口的示例,以及所有实现类似以下内容的示例: public string Error => null; public string this[string columnName] { get { string res = null; switch (columnName) { case "FirstName": if (FirstName != "
IDataErrorInfo
接口的示例,以及所有实现类似以下内容的示例:
public string Error => null;
public string this[string columnName] {
get {
string res = null;
switch (columnName) {
case "FirstName":
if (FirstName != "test") res = "First Name Invalid!";
break;
case "LastName":
if (LastName != "test") res = "Last Name Invalid!";
break;
}
return res;
}
}
公共字符串错误
的目的是什么?如何使用它而不是返回null
?在xaml
中,一些示例使用预定义的样式定位TextBox
,其他示例使用标签
或工具提示
,如下所示:
<TextBox x:Name="Fname"
Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
ToolTip="{Binding ElementName=Fname, Path=(Validation.Errors)[0].ErrorContent}" />
<Label Content="{Binding ElementName=Fname, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
要验证整个模型,我只需要调用:
Validator.TryValidateObject(this, new ValidationContext(this), null, true);
在
ICommand
的CanExecute
中。此示例也没有使用公共字符串错误错误属性,因此您只需关心实现索引器
顺便说一下,从.NET Framework 4.5开始,建议实现并使用INotifyDataErrorInfo
接口,而不是IDataErrorInfo
Validator.TryValidateObject(this, new ValidationContext(this), null, true);