1024programmer Asp.Net How to use IDataErrorInfo to implement data verification

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

In this[string columnName] method, we can determine whether the value of the column is legal based on the passed in column name:
���—If it is illegal, the corresponding error message will be returned.
——If null or an empty string is returned, it means that the value of the attribute is valid.

Error method: We can determine whether the value of the entire data model is legal, and if not, return the corresponding error message.

Notice:
If an error message is returned in this[string columnName] method, the Error method will not be called.
The Error method will be called only if the values ​​of all properties are legal.
Therefore, when implementing the IDataErrorInfo interface, we need to determine whether the value of the attribute is legal as much as possible in this [string columnName] method to reduce the number of calls to the Error method.


④ When entering data on the interface, if the verification fails, a red exclamation point icon will be displayed next to the control, and an error message will be displayed by hovering the mouse over the icon.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-use-idataerrorinfo-to-implement-data-verification/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

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
首页
微信
电话
搜索