WPF Enterprise Library verification

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  WPF bring your own validation function, and we go to inherit from ValidationRule and override the Validate method, our custom validation rules. If we have n a need to verify the place, is it right? Will override the n method in Validate, then verify the class file will be many.

  Another is the successor to the IDataErrorInfo, this is very good, write their own good rules in each attribute, but also realizes the separation with UI, but as I said, you have to write.

  Using Enterprise Library, we can verify easily. Of course he has many functions, we just said his validation function. This on the Internet you can download and install it, and then cited his DLL, what is the verification function so refers to the two one on the line

The best time to remember. Install your installation path, or installed to is not easy to find the two DLL. I use 5 his support for.Net4.0 and 3.5.

  1Then we need to write a class

public  class BaseValidationEntity<T> :  IDataErrorInfo


//Returns null or an empty string, the validation error

//The Error property is used to indicate the object error, IDataErrorInfo

public string Error


get { return null; }


//The indexer is used to indicate a single attribute level error, the realization of IDataErrorInfo, using the indexer benefits are what I don't understand, said he can handle objects like an array, I just don't know why since object like an array processing

public string this[string name]


//This return to verify the information, if no return value is returned to the null, that is not validated, if have the return value that returns the first value is displayed on the page, verify the information, for example, can not be empty.



string result = null;

Validator<T> validator = ValidationFactory.CreateValidator<T>();

ValidationResults results = validator.Validate(this);

if (results.Count > 0)


return results.First().Message;

//foreach (var item in results)


// result += item.Message;



return result;



//This is used to check whether the verification, if no value is returned, so is explained by true

public bool Valid()


Validator<T> validator = ValidationFactory.CreateValidator<T>();

ValidationResults results = validator.Validate(this);

return results.Count == 0;



This class we need to inherit IDataErrorInfo, because when we display an error message on the page need to use him.

2Then what pages need verification we inherited it.

For example


We write tag attributes above need validation, he mainly has following several validation

ContainsCharactersValidatorAttribute——Whether a string containing the verification

DateTimeRangeValidatorAttribute——Time range

DomainValidatorAttribute——Through the test object, determine whether it belongs to a set

EnumConversionValidatorAttribute——To check whether a string can be converted into an item in the enumeration

HasSelfValidationAttribute——Self verification, can only be used for the SelfValidationAttribute service class,

IgnoreNullsAttribute——Nullable verification

NotNullValidatorAttribute——Not null verification

ObjectCollectionValidatorAttribute——Object set type validation

ObjectValidatorAttribute——Object type validation

PropertyComparisonValidatorAttribute——Property comparison and validation

RangeValidatorAttribute——Scope Verification, to judge whether the object is in a specified range

RegexValidatorAttribute——Regular expression validation

RelativeDateTimeValidatorAttribute——Check whether the date to a range of relative to the current time or date

SelfValidationAttribute——To call a method of self verification

StringLengthValidatorAttribute——The length of the string verification

TypeConversionValidatorAttribute——Type conversion verification, whether can be converted to the specified type

ValidatorComposition——Verify the combiner, combining different validation together

Of course he also support custom validation extension, this is very good.

3Then we need to put the wrong information presented on the page

So that you can,

4Of course, if not validated cannot let him to submit information, we can

When we each attribute change to calling the Valid method is validated in the base class, if true did not return messages. You can continue to operate below, the Login method you when practicing can directly write the click event, I used Caliburn.Micro, so I wrote this.

WPF verification is very flexible, this is I feel a verification better. We have a good way, you can discuss.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Boyce at November 14, 2013 - 4:42 PM