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