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; }…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索