/// <summary>/// Author:TerryLee/// [url]http://www.cnblogs.com/Terrylee[/url]/// </summary>public class Person : INotifyPropertyChanged{ public event PropertyChangedEventHandler PropertyChanged; private int _age; public int Age { get { return _age; } set { if (value < 0) throw new Exception("年龄输入不合法!"); _age = value; if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs("Age")); } } } private String _name = "Terry"; public String name { get { return _name; } set { if (value.Length < 4) throw new Exception("姓名输入不合法!"); _name = value; if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs("name")); } } } public voID NotifyPropertyChanged(String propertyname) { if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyname)); } }}编写数据绑定,如下代码所示,设置NotifyOnValIDationError和ValIDatesOnExceptions属性为true,并且定义BindingValIDationError事件:
<!-- [url]http://www.cnblogs.com/Terrylee[/url]--><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,ValIDatesOnExceptions=true}" BindingValIDationError="txtAge_BindingValIDationError"> </TextBox> <my:Message x:name="messageAge"></my:Message></StackPanel>实现BindingValIDationError事件,在这里可以根据ValIDationErrorEventAction来判断如何进行处理,在界面给出相关的提示信息等,如下代码所示:
/// <summary>/// Author:TerryLee/// [url]http://www.cnblogs.com/Terrylee[/url]/// </summary>voID txtAge_BindingValIDationError(object sender,ValIDationErrorEventArgs e){ if (e.Action == ValIDationErrorEventAction.Added) { messageAge.Text = e.Error.Exception.Message; messageAge.ValIDation = false; } else if (e.Action == ValIDationErrorEventAction.Removed) { messageAge.Text = "年龄验证成功"; messageAge.ValIDation = true; }}好了,现在来看一下最后的验证效果,如下图所示: 通过这样的方式,我们就可以 在Silverlight中对于数据输入做验证了。 总结 本文介绍了如何在Silverlight应用程序进行数据验证,希望对大家有所帮助。更多Silverlight 2的文章请参考 Silverlight 2 相关文章汇总。 本文首发于 博客园,作者 TerryLee,原文地址: 技巧:在Silverlight应用程序中进行数据验证 总结
以上是内存溢出为你收集整理的技巧:在Silverlight应用程序中进行数据验证全部内容,希望文章能够帮你解决技巧:在Silverlight应用程序中进行数据验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)