Siverlight asynchronous data verification 2

Source: Internet
Author: User

This article describes the DataAnnotation verification mechanism, using the authentication mechanism provided by the RIA service,

Namespace needs to be introduced:

Using System. ComponentModel. DataAnnotations;

This document takes the username and password as an example to describe the required verification method.

The verification class is as follows:

 

1 private string _ userName;
2 [Required (ErrorMessage = "Required")]
3 public string UserName
4 {
5 get {return _ userName ;}
6 set
7 {
8 if (_ userName! = Value)
9 {
10 // _ userName = value;
11 // NotifyPropertyChanged ("UserName ");
12
13 var tmpValidator = new ValidationContext (this, null, null );
14 tmpValidator. MemberName = "UserName ";
15 Validator. ValidateProperty (value, tmpValidator );
16 _ userName = value;
17
18}
1920
21}
22}

The client settings are as follows:

Step 1:

Xmlns: local = "clr-namespace: SilverlightApplication2"

Step 2

<Local: User x: Key = "userDataContext"/>

Step 3 bind the data source

 

1 <TextBox Grid. column = "1" Height = "30" HorizontalAlignment = "Left" Margin = "28, 57, 0, 0 "Name =" textBox1 "DataContext =" {Binding Source = {StaticResource userDataContext} "Text =" {Binding UserName, Mode = TwoWay, NotifyOnValidationError = True, validatesOnExceptions = True} "verticalignment =" Top "Width =" 158 "/>
2 <TextBox Height = "30" HorizontalAlignment = "Left" Margin = "28, 55, 0, 0 "Name =" textBox2 "DataContext =" {Binding Source = {StaticResource userDataContext} "Text =" {Binding Password, Mode = TwoWay, NotifyOnValidationError = True, validatesOnExceptions = True} "verticalignment =" Top "Width =" 158 "Grid. column = "1" Grid. row = "1"/>

 

Step 4: Capture

 

1 private void LayoutRoot_BindingValidationError (object sender, ValidationErrorEventArgs e)
2 {
3 if (e. Action = ValidationErrorEventAction. Added)
4 {
5 (e. OriginalSource as Control). Background = new SolidColorBrush (Colors. Yellow );
6 tbMessage. Text = e. Error. Exception. Message;
7}
8
9 if (e. Action = ValidationErrorEventAction. Removed)
10 {
11 (e. OriginalSource as Control). Background = new SolidColorBrush (Colors. White );
12 tbMessage. Text = "";
13}
14}

 

The effect is as follows:

Download Demo

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.