WPF data validation

Source: Internet
Author: User

When filling out a form, you need to verify the content, check that the data meets the requirements, such as the length of the string, the format of the date, the number, and so on. WPF supports custom validation rules and provides visual feedback to notify users when an invalid value is entered.

The following example demonstrates a process for simulating employee information entry, an integer with an employee's age greater than 18, a number greater than 2500, and a red exclamation mark after the text box if the input is incorrect, and when the mouse moves to the error text box, a prompt message prompts the user for the correct input format.

Validation of data

You can inherit the ValidationRule class, override the Validate method, implement a custom validation rule

The following are the implementations of the Age validation rule:

     Public classAgerangerule:validationrule {Private int_min;Private int_max; PublicAgeRangeRule () {} Public intMin {Get{return_min; }Set{_min =value; }        } Public intMax {Get{return_max; }Set{_max =value; }        } Public OverrideValidationresult Validate (Object value, CultureInfo CultureInfo) {intAge = 0;Try{if(((string)value). Length > 0) Age = Int32.Parse ((String)value); }Catch(Exception e) {return NewValidationresult (false, "the number entered is invalid! "); }if(Age < Min) | |            (Age > Max)) {return NewValidationresult (false,                  "The age range you enter must be in:"+ Min +" - "+ Max +"between"); }Else{return NewValidationresult (true,NULL); }        }    }
View Code

WPF data validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.