silverlight客户端验证控件ValidationSummary及DescriptionViewer


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(valuenew ValidationContext(thisnullnull) { MemberName = "Age" });
2.
DescriptionViewer
ValidationSummary
这两个控件如果没有有工具箱中找到则可以"右键-选择项"在弹出的对话框中找到对应的选中后点确定即可.


3.DescriptionViewer

命名空间:   System.Windows.Controls

程序集:  System.Windows.Controls.Data.Input(在 System.Windows.Controls.Data.Input.dll 中)


XAML代码:

 <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
 x:Class="ValidateTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<StackPanel x:Name="LayoutRoot" Background="White">
      <StackPanel Orientation="Horizontal">
           <TextBox x:Name="txtId" Text="{Binding Id,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
           <sdk:DescriptionViewer Target="{Binding ElementName=txtId}"/>
      </StackPanel>
<StackPanel Orientation="Horizontal">
       <TextBox x:Name="txtName" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
       <sdk:DescriptionViewer Target="{Binding ElementName=txtName}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
        <TextBox x:Name="txtAge" Text="{Binding Age,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox>
        <sdk:DescriptionViewer Target="{Binding ElementName=txtAge}"/>
</StackPanel>
<Button Content="提交" Click="Button_Click"/>
<sdk:ValidationSummary />
</StackPanel>
 </UserControl>


CS代码:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Animation;
 using System.Windows.Shapes;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
 namespace ValidateTest
 {
     public partial class MainPage : UserControl
     {
         ViewModel data;
         public MainPage()
         {
             InitializeComponent();
             data = new ViewModel() { Id = 3, Name = "gaoshengjie", Age = 20 };
             this.DataContext = data;
             
         }
 
         private void Button_Click(object sender, RoutedEventArgs e)
         {
             MessageBox.Show(data.Id.ToString());
         }
     }
     public class ViewModel:INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;
         public void NotifyPropertyChanged(string propertyName)
         {
             if (this.PropertyChanged != null)
             {
                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
         private int id;
         private string name;
         private int age;
         [Display(Name = "年龄", Description = "必须在18以上")]
         [Range(18, 150,ErrorMessage="不能小于18岁!")]
         [Required(ErrorMessage="不能为空!")]
         public int Age
         {
             get 
             { 
                 return age; 
             }
             set 
             {
                 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Age" });
                 age = value; 
                 this.NotifyPropertyChanged("Age"); 
             }
         }
         [Display(Name = "姓名", Description = "不能为空")]
         [RegularExpression(@"^gao[a-zA-Z]{1,50}$", ErrorMessage = "必须以gao开头!")]
         [Required(ErrorMessage = "不能为空!")]
         public string Name
         {
             get 
             { 
                 return name;
             }
             set 
             {
                 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
                 name = value;
                 this.NotifyPropertyChanged("Name"); 
             }
         }
         [Display(Name = "编号", Description = "唯一标识,必须大于1!")]
         [Range(1, 9999, ErrorMessage = "范围在1-9999")]
         [Required(ErrorMessage = "不能为空!")]
         public int Id
         {
             get 
             { 
                 return id; 
             }
             set 
             {
                 Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Id" });
                 id = value; 
                 this.NotifyPropertyChanged("Id"); 
             }
         }
     }
 }

提供了如下 验证 控件
1.RequiredValidator:非空验证
2.LengthValidator:长度验证
3.PhoneValidator:电话号码验证,可以在PhoneValidator类构造函数中修改验证正则表达式来实现自定义的验证规则。
4.RangeValidator:范围验证,例如验证年龄的范围。
5.RegexValidator:正则表达式验证,验证是否符合指定的表达式。
6.SSNValidator:美国社会 安全号验证,其实这个是继承RegexValidator实现的,我用不上,把它改成了身份证号码验证。
7.还提供了一个过滤器:TextBoxFilterService,用来控制textbox的输入值类型,例如PositiveInteger正整数、Integer、PositiveDecimal、Decimal、Alpha(阿尔法,不知怎么用)。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值