How to use IDataErrorInfo to implement data verification
How to use IDataErrorInfo to implement data verification 1. Definition: ValidatesOnDataErrors is a way to implement data validation in WPF and can be enabled by setting the property ValidatesOnDataErrors to True in XAML. 2. Use: ① Implement the IDataErrorInfo interface in ViewModel, which defines two properties: Error and Item[string columnName] ——The Error property returns the description information of all errors in ViewModel; ——The Item[string columnName] attribute returns the error description information of the specified attribute. ② Set the ValidatesOnDataErrors property of Binding to True in XAML, for example: <TextBox Text=” {Binding Name, ValidatesOnDataErrors=True} ” /> ③ Set annotations in ViewModel on the attributes that need to be verified, for example: public class Person : IDataErrorInfo { public string span> Name { get; set; } public int span> Age { get; set; } public string span> this[string columnName] { get { string error = null ; switch (columnName) { case ” span>Name” : if (string.IsNullOrEmpty(Name)) error = “Name Cannot be empty” ; break; case ” span>Age” : if (Age < 0 || Age > 120) error = “Age Must be between 0 and 120”; break; } return error; } } public string span> Error { get { return null; }…