silverlight客户端验证控件ValidationSummary及DescriptionViewer
Posted on 2010-09-08 15:55 小高好孩子 阅读(744) 评论(0) 收藏 举报
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.ComponentModel;
13 using System.ComponentModel.DataAnnotations;
14 namespace ValidateTest
15 {
16 public partial class MainPage : UserControl
17 {
18 ViewModel data;
19 public MainPage()
20 {
21 InitializeComponent();
22 data = new ViewModel() { Id = 3, Name = "gaoshengjie", Age = 20 };
23 this.DataContext = data;
24
25 }
26
27 private void Button_Click(object sender, RoutedEventArgs e)
28 {
29 MessageBox.Show(data.Id.ToString());
30 }
31 }
32 public class ViewModel:INotifyPropertyChanged
33 {
34 public event PropertyChangedEventHandler PropertyChanged;
35 public void NotifyPropertyChanged(string propertyName)
36 {
37 if (this.PropertyChanged != null)
38 {
39 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
40 }
41 }
42 private int id;
43 private string name;
44 private int age;
45 [Display(Name = "年龄", Description = "必须在18以上")]
46 [Range(18, 150,ErrorMessage="不能小于18岁!")]
47 [Required(ErrorMessage="不能为空!")]
48 public int Age
49 {
50 get
51 {
52 return age;
53 }
54 set
55 {
56 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
57 age = value;
58 this.NotifyPropertyChanged("Age");
59 }
60 }
61 [Display(Name = "姓名", Description = "不能为空")]
62 [RegularExpression(@"^gao[a-zA-Z]{1,50}$", ErrorMessage = "必须以gao开头!")]
63 [Required(ErrorMessage = "不能为空!")]
64 public string Name
65 {
66 get
67 {
68 return name;
69 }
70 set
71 {
72 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
73 name = value;
74 this.NotifyPropertyChanged("Name");
75 }
76 }
77 [Display(Name = "编号", Description = "唯一标识,必须大于1!")]
78 [Range(1, 9999, ErrorMessage = "范围在1-9999")]
79 [Required(ErrorMessage = "不能为空!")]
80 public int Id
81 {
82 get
83 {
84 return id;
85 }
86 set
87 {
88 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Id" });
89 id = value;
90 this.NotifyPropertyChanged("Id");
91 }
92 }
93 }
94 }
95
代码
1 <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
2 x:Class="ValidateTest.MainPage"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7 mc:Ignorable="d"
8
9 d:DesignHeight="300" d:DesignWidth="400">
10
11 <StackPanel x:Name="LayoutRoot" Background="White">
12 <StackPanel Orientation="Horizontal">
13 <TextBox x:Name="txtId" Text="{Binding Id,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
14 <sdk:DescriptionViewer Target="{Binding ElementName=txtId}"/>
15 </StackPanel>
16 <StackPanel Orientation="Horizontal">
17 <TextBox x:Name="txtName" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
18 <sdk:DescriptionViewer Target="{Binding ElementName=txtName}"/>
19 </StackPanel>
20 <StackPanel Orientation="Horizontal">
21 <TextBox x:Name="txtAge" Text="{Binding Age,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
22 <sdk:DescriptionViewer Target="{Binding ElementName=txtAge}"/>
23 </StackPanel>
24 <Button Content="提交" Click="Button_Click"/>
25 <sdk:ValidationSummary />
26 </StackPanel>
27 </UserControl>
28
需要注意的是:
1.命名空间的引用
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
引用以后才可以使用以下内容
[Display(Name = "年龄", Description = "必须在18以上")]
[Range(18, 150,ErrorMessage="不能小于18岁!")]
[Required(ErrorMessage="不能为空")]s
[RegularExpression(@"^gao[a-zA-Z]{1,50}$", ErrorMessage = "必须以gao开头!")]
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });2.DescriptionViewerValidationSummary这两个控件如果没有有工具箱中找到则可以"右键-选择项"在弹出的对话框中找到对应的选中后点确定即可.

浙公网安备 33010602011771号