Silverlight数据验证实现技巧分享

开发 开发工具
Silverlight数据验证的实现对于刚刚学习Silverlight开发工具不久的朋友来说可能不太好掌握。希望这篇文章可以为大家带来一些帮助。

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事件:

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

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

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

 

 

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

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-04 14:14:43

Silverlight

2009-12-29 17:56:47

Silverlight

2009-12-30 18:23:13

Silverlight

2009-12-31 17:00:40

Silverlight

2010-01-04 14:35:55

Silverlight

2009-12-29 16:08:41

Silverlight

2009-12-30 13:37:24

Silverlight

2009-12-30 10:25:03

Silverlight

2009-12-16 15:46:41

Ruby on rai

2010-01-04 16:30:06

Silverlight

2009-12-29 17:34:52

Silverlight

2009-12-30 17:19:09

Silverlight

2009-12-29 18:34:21

Silverlight

2009-03-31 13:12:05

ASP.NETMVC表单验证

2009-03-03 09:00:57

Silverlight数据验证UI控件

2011-04-08 15:40:01

Oracle认证

2010-01-28 17:12:45

Android闪屏

2010-03-05 13:29:00

Python增量备份

2009-12-10 10:24:24

PHP写入文件

2011-02-21 17:15:14

SilverlightNEY
点赞
收藏

51CTO技术栈公众号