博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight数据验证
阅读量:7071 次
发布时间:2019-06-28

本文共 2704 字,大约阅读时间需要 9 分钟。

说明:本文出处:

前台:

<TextBox Name="tb1" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True}" Height="100" Width="100"/>

后台:

 

Person p = new Person();public MainPage(){    InitializeComponent();    p.Name = "123";    tb1.DataContext = p;}public class Person : INotifyPropertyChanged{    private string name;    public string Name    {        get { return name; }        set        {            if (value.Length > 3)            {                throw new Exception("不能超过三个字!");            }            name = value;            NotifyChange("Name");        }    }    public event PropertyChangedEventHandler PropertyChanged;    private void NotifyChange(string propertyName)    {        if (PropertyChanged != null)        {            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));        }    }}

  

2.验证TextBox内容不超过指定长度,不失去焦点验证。只需要在上例的基础上为TextBox添加TextChanged事件,并且在事件里通知数据源即可。

private void tb1_TextChanged(object sender, TextChangedEventArgs e)

{     System.Windows.Data.BindingExpression expresson = (sender
as TextBox).GetBindingExpression(TextBox.TextProperty);     expresson.UpdateSource(); }

对验证提示,可以进行样式的设置。

前台添加NotifyOnValidationError属性和BindingValidationError事件

<TextBox Name="tb1" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Height="100" Width="100" TextChanged="tb1_TextChanged" BindingValidationError="tb1_BindingValidationError"/>

后台实现BindingValidationError事件

 

View Code
1 private void tb1_BindingValidationError(object sender, ValidationErrorEventArgs e) 2 { 3     if (e.Action == ValidationErrorEventAction.Added) 4     { 5         (sender as TextBox).Background = new SolidColorBrush(Colors.Red); 6  7     } 8     else if (e.Action == ValidationErrorEventAction.Removed) 9     {10         (sender as TextBox).Background = new SolidColorBrush(Colors.White);11     }12 13 }

3.标注的方式验证。要添加System.ComponentModel.DataAnnotations.dll引用,并且将数据源的类定义修成为如下形式:

View Code
1 public class Person : INotifyPropertyChanged 2 { 3     private string name; 4     [StringLength(3, ErrorMessage = "不能超过3个字,这是标注的方式验证!")] 5     public string Name 6     { 7         get { return name; } 8         set 9         {10             Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });11             name = value;12             NotifyChange("Name");13         }14     }15     public event PropertyChangedEventHandler PropertyChanged;16     private void NotifyChange(string propertyName)17     {18         if (PropertyChanged != null)19         {20             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));21         }22     }23 24 }

 

转载于:https://www.cnblogs.com/zxbzl/archive/2013/03/10/2952703.html

你可能感兴趣的文章
Shell中cut用法
查看>>
Python 文件操作
查看>>
AC620教程 第十五节 8位7段数码管驱动设计与验证
查看>>
第十章 Libgdx音频之概述
查看>>
【CSS】使用CSS控制文字过多自动省略号
查看>>
【maven】maven创建web项目-pom文件提示web.xml is missing and <failOnMissingWebXml> is set to true...
查看>>
2016数据库考试题
查看>>
使用Zipalign工具优化Android APK应用记录
查看>>
LinearLayout属性baselineAligned的作用及baseline
查看>>
事件怎么看?
查看>>
css页面字体替换源代码和页面显示不一样问题解决
查看>>
读书笔记--精通CSS高级Web标准解决方案(五)---链接样式
查看>>
网络服务
查看>>
LaTeX新人教程,30分钟从完全陌生到基本入门
查看>>
nginx关闭日志
查看>>
我的自白,白衣轩重开
查看>>
angularjs-select2的使用
查看>>
vs2010连接远程数据库出现程序崩溃
查看>>
bzoj 1194
查看>>
69期-Java SE-001_Java概述-001-002
查看>>