Silverlight数据验证实现技巧分享
创始人
2024-06-18 20:41:08
0

Silverlight开发工具对于编程人员来说是一个非常有用的多媒体处理平台。其中有很多功能与应用技巧值得我们去深入的研究。在这里我们就为大家介绍一下有关Silverlight数据验证的实现方法。#t#

首先我们编写一个简单的业务类,由于数据绑定验证只能在双向绑定中,所以这里需要实现INotifyPropertyChanged接口,如下Silverlight数据验证代码所示,在set设置器中我们对于数据的合法性进行检查,如果不合法则抛出一个异常:

 

  1. /// < summary> 
  2. /// Author:TerryLee  
  3. /// http://www.cnblogs.com/Terrylee  
  4. /// < /summary> 
  5. public class Person : 
    INotifyPropertyChanged  
  6. {  
  7. public event PropertyChanged
    EventHandler PropertyChanged;  
  8. private int _age;  
  9. public int Age  
  10. {  
  11. get { return _age; }  
  12. set {  
  13. if (value <  0)  
  14. throw new Exception("年龄输入不合法!");  
  15. _age = value;  
  16. if (PropertyChanged != null)  
  17. {  
  18. PropertyChanged(this, new 
    PropertyChangedEventArgs("Age"));  
  19. }  
  20. }  
  21. }  
  22. private String _name = "Terry";  
  23. public String Name  
  24. {  
  25. get { return _name; }  
  26. set {  
  27. if (value.Length <  4)  
  28. throw new Exception("姓名输入不合法!");  
  29. _name = value;  
  30. if (PropertyChanged != null)  
  31. {  
  32. PropertyChanged(this, new 
    PropertyChangedEventArgs("Name"));  
  33. }  
  34.  
  35. }  
  36. }  
  37. public void NotifyPropertyChanged
    (String propertyName)  
  38. {  
  39. if (PropertyChanged != null)  
  40. {  
  41. PropertyChanged(this, new Property
    ChangedEventArgs(propertyName));  
  42. }  
  43. }  

编写Silverlight数据验证,如下代码所示,设置NotifyOnValidationError和ValidatesOnExceptions属性为true,并且定义BindingValidationError事件:

  1. < !--  
  2. http://www.cnblogs.com/Terrylee  
  3. --> 
  4. < StackPanel Orientation=
    "Horizontal" Margin="10"> 
  5. < TextBox x:Name="txtName" 
    Width="200" Height="30" 
  6. Text="{Binding Name,Mode=TwoWay,  
  7. NotifyOnValidationError=true,  
  8. ValidatesOnExceptions=true}"  
  9. BindingValidationError="txtName_
    BindingValidationError"> 
  10. < /TextBox> 
  11. < my:Message x:Name="messageName">
    < /my:Message> 
  12. < /StackPanel> 
  13. < StackPanel Orientation=
    "Horizontal" Margin="10"> 
  14. < TextBox x:Name="txtAge" 
    Width="200" Height="30" 
  15. Text="{Binding Age,Mode=TwoWay,  
  16. NotifyOnValidationError=true,  
  17. ValidatesOnExceptions=true}"  
  18. BindingValidationError="txtAge_
    BindingValidationError"> 
  19. < /TextBox> 
  20. < my:Message x:Name=
    "messageAge">< /my:Message> 
  21. < /StackPanel> 

实现BindingValidationError事件,在这里可以根据ValidationErrorEventAction来判断如何进行处理,在界面给出相关的提示信息等,如下Silverlight数据验证代码所示:

  1. /// < summary> 
  2. /// Author:TerryLee  
  3. /// http://www.cnblogs.com/Terrylee  
  4. /// < /summary> 
  5. void txtAge_BindingValidationError
    (object sender, ValidationErrorEventArgs e)  
  6. {  
  7. if (e.Action == ValidationError
    EventAction.Added)  
  8. {  
  9. messageAge.Text = e.Error.
    Exception.Message;  
  10. messageAge.Validation = false;  
  11. }  
  12. else if (e.Action == Validation
    ErrorEventAction.Removed)  
  13. {  
  14. messageAge.Text = "年龄验证成功";  
  15. messageAge.Validation = true;  
  16. }  

 

 

通过这样的方式,我们就可以实现Silverlight数据验证。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...