.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);