自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Silverlight數(shù)據(jù)驗證實現(xiàn)技巧分享

開發(fā) 開發(fā)工具
Silverlight數(shù)據(jù)驗證的實現(xiàn)對于剛剛學習Silverlight開發(fā)工具不久的朋友來說可能不太好掌握。希望這篇文章可以為大家?guī)硪恍椭?/div>

Silverlight開發(fā)工具對于編程人員來說是一個非常有用的多媒體處理平臺。其中有很多功能與應用技巧值得我們?nèi)ド钊氲难芯?。在這里我們就為大家介紹一下有關(guān)Silverlight數(shù)據(jù)驗證的實現(xiàn)方法。#t#

首先我們編寫一個簡單的業(yè)務類,由于數(shù)據(jù)綁定驗證只能在雙向綁定中,所以這里需要實現(xiàn)INotifyPropertyChanged接口,如下Silverlight數(shù)據(jù)驗證代碼所示,在set設(shè)置器中我們對于數(shù)據(jù)的合法性進行檢查,如果不合法則拋出一個異常:

 

  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數(shù)據(jù)驗證,如下代碼所示,設(shè)置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> 

實現(xiàn)BindingValidationError事件,在這里可以根據(jù)ValidationErrorEventAction來判斷如何進行處理,在界面給出相關(guān)的提示信息等,如下Silverlight數(shù)據(jù)驗證代碼所示:

  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. }  

 

 

通過這樣的方式,我們就可以實現(xiàn)Silverlight數(shù)據(jù)驗證。

責任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-01-04 14:14:43

Silverlight

2009-12-30 18:23:13

Silverlight

2009-12-29 17:56:47

Silverlight

2009-12-31 17:00:40

Silverlight

2010-01-04 14:35:55

Silverlight

2009-12-29 16:08:41

Silverlight

2009-12-16 15:46:41

Ruby on rai

2009-12-30 13:37:24

Silverlight

2009-12-30 10:25:03

Silverlight

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數(shù)據(jù)驗證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技術(shù)棧公眾號