WPF outsourcing company-WPF Outsourcing Consulting: asynchronous data verification, one of the secrets of WPF 4.5, a dynamic node flying software in Beijing

Source: Internet
Author: User

Haserrors: a read-only Boolean attribute that identifies whether an error is verified when an object is a whole;

Geterrors: This method returns a verification error for a given attribute;

Errorschanged: when a new error -- or a lack of error -- is detected. This event is generated.

Note that if you return false to the haserrors attribute, binding will occur if no error occurs.

How to Use wpf4.5?

You must set the validatesondataerrors attribute to true for each object bound to you.

In the example, a form is created, and the property of the display object is named "person ". Here we will explain how to enable inotifydataerrorinfo verification in binding:

<Textbox text = "{binding name, mode = twoway, validatesonpolicydataerrors = true}"/>

The binding registers itself as an errorschanged event. When a property binding event occurs, these controls automatically display an error. As mentioned above, this will only occur when haserrors is set to true.

A delay occurred when verifying the person class. Verification will occur when any attribute is set, but it will be delayed for several seconds due to "waitsecondsbeforevalidation.

Save the error under each attribute and provide it using the geterros method. If an error occurs, the haserrors attribute returns true. The following is the implementation code:

Public System. Collections. ienumerable geterrors (string propertyname)
{
List <string> errorsforname;
_ Errors. trygetvalue ("name", out errorsforname );
Return errorsforname;
}

Public bool haserrors
{
Get {return _ errors. Values. firstordefault (L => L. Count> 0 )! = NULL ;}
}

Private dictionary <string, list <string> _ errors =
New Dictionary <string, list <string> ();

Private void validate ()
{
Task waittask = new task () => thread. Sleep (
Timespan. fromseconds (waitsecondsbeforevalidation )));
Waittask. continuewith (_) => realvalidation ());
Waittask. Start ();
}

Private object _ Lock = new object ();
Private void realvalidation ()
{
Lock (_ Lock)
{
// Validate name
List <string> errorsforname;
If (! _ Errors. trygetvalue ("name", out errorsforname ))
Errorsforname = new list <string> ();
Else errorsforname. Clear ();

If (string. isnullorempty (name ))
Errorsforname. Add ("the name can't be null or empty .");

_ Errors ["name"] = errorsforname;
If (errorsforname. Count> 0) raiseerrorschanged ("name ");

// Validate age
List <string> errorsforage;
If (! _ Errors. trygetvalue ("Age", out errorsforage ))
Errorsforage = new list <string> ();
Else errorsforage. Clear ();

If (age <= 0)
Errorsforage. Add ("the age must be greater than zero .");

_ Errors ["Age"] = errorsforage;
If (errorsforage. Count> 0) raiseerrorschanged ("Age ");
}

Finally, I created a raiseerrorschanged method to reduce the verification event error:

Public event eventhandler <dataerrorschangedeventargs> errorschanged;
Public void raiseerrorschanged (string propertyname)
{
Eventhandler <dataerrorschangedeventargs> handler = errorschanged;
If (handler = NULL) return;
VaR Arg = new dataerrorschangedeventargs (propertyname );
Handler. Invoke (this, ARG );
}
Related Article

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.