Silverlight之我见——数据批示(2)
接着上一回的话题,继续来研究数据批示特性,先拿简单的RageAttribute来弄弄,接着上次的示例,添加一个Age属性,并加上RangeAttribute。
- [Range(20,60,ErrorMessage="年龄范围在20与60之间。")]
- [Display(Name="年龄",Description="歌手年龄。")]
- public int Age { get; set; }
接着把XAML也补完整。
- <sdk:Label x:Name="lbAge" Target="{Binding ElementName=txtAge}" Grid.Column="0"
- Grid.Row="2" FontSize="14" Margin="1,1,20,1"/>
- <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
- <TextBox x:Name="txtAge" Margin="1,1" Width="165"
- Text="{Binding Age}"/>
- <sdk:DescriptionViewer Target="{Binding ElementName=txtAge}"/>
- </StackPanel>
- int m_Age = 20;
- [Range(20, 60, ErrorMessage = "年龄范围在20与60之间。")]
- [Display(Name = "年龄", Description = "歌手年龄。")]
- public int Age
- {
- get { return this.m_Age; }
- set
- {
- Validator.ValidateProperty(value,
- new ValidationContext(this, null, null) { MemberName = "Age" });
- this.m_Age = value;
- }
- }
- <TextBox x:Name="txtAge" Margin="1,1" Width="165"
- Text="{Binding Age,Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}"/>
- <sdk:ValidationSummary Grid.Row="3" Grid.ColumnSpan="2"/>

山重水复疑无路,柳暗花明会成功,这时候,你在项目上右击,选择“在浏览器中查看”,啊,眼前一亮,出来了。

现在,总结一下方法:
1、在定义公共属性时加上对应的特性,如RangeAttribute;
2、在属性的set访问器上调用Validator的ValidateProperty方法;
3、在XAML或前台绑定时,将Binding 的Mode设置为TwoWay,ValidatesOnExceptions和NotifyOnValidationError设置为True;
4、添加ValidationSummary控件以显示错误列表,当然,不加也可以,看看下面的截图。

5、在非debug模式下运行程序。
Silverlight之我见——数据批示(2)的更多相关文章
- Silverlight之我见——数据批示(1)
第一次听到这个概念,你是否有点陌生?MSDN上也没有特意的去说明.不要看到这个名词不太熟悉,其实数据批示,玩过C#的人都会非常熟悉,所谓数据批示,其本质就是特性(Attribute),怎么样,现在有点 ...
- 使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置)
原文:使用Socket通信实现Silverlight客户端实时数据的获取(模拟GPS数据,地图实时位置) 在上一篇中说到了Silverlight下的Socket通信,在最后的时候说到本篇将会结合地图. ...
- Silverlight之我见——DataGrid数据验证
<UserControl x:Class="DataValidationSample.MainPage" xmlns="http://schemas.microso ...
- Silverlight之我见
好长时间没搞Silverlight方面的开发了,原本都以为自己早已忘记,然而前阵子(确切一点说,是挺长时间以前了)的时候,发布Windows10的时候,微软宣布新的浏览器将重新开发,关键是后半句引起了 ...
- silverlight中datagrid数据到处excel
首先新建一个DataGrdiExtensions类,代码为: public static class DataGridExtensions { /// <summary> /// 导出dg ...
- Silverlight DataGrid自适应数据
silverlight的DataGrid如果改成fill模式 那么当数据超长也不会出现横向滚动条 如果改成按单元格模式 如果数据较短又会出现空白的地方 所以我自己写了个算法 目前实现了以list为数据 ...
- Silverlight保存大数据到WCF出现Not Found问题
Silveright调用WCF保存大数据时,直接在客户端报“ 远程服务器返回了错误:Not Found ”问题,少量是没问题,如下图: 后面在这个文章中找到解决办法: http://www.cnblo ...
- silverlight,动态数据集合中,移除动态集合自身的内容
在xaml的页面上创建一个x:Name为_list1的ListBox,其中ListBox里面的每一项是ListBoxItem if (_list1.SelectedItem == null)//如果_ ...
- silverlight中DataGrid数据高亮显示
效果如图所示, <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.W ...
随机推荐
- HDU 5489 Difference of Clustering 图论
Difference of Clustering Problem Description Given two clustering algorithms, the old and the new, y ...
- PHP数字左侧自动补零
1.输出数字为001,002... <?php $number=0; if($number<100) { $number=$number+1; $txt=sprintf("%03 ...
- how to use webpart container in kentico
https://docs.kentico.com/k11/developing-websites/developing-websites-using-the-portal-engine/using-w ...
- Ubuntu 查看当前目录使用的总空间大小
查看当前目录使用的总空间大小 du -h --max-depth=0 #du -h --max-depth=0 217M . 查看当前目录使用总空间的大小以及当前目录下一级文件及文件夹各自使用的总空间 ...
- Sql2005常用函数大全
--聚合函数use pubsgoselect avg(distinct price) --算平均数from titleswhere type='business'go use pubsgoselect ...
- influxdb入门——和mongodb一样可以动态增加字段
./influxd [--config yourconfigfile 2> /dev/null] 之所以重定向 因为默认log是stderr 再启动客户端./influx > CREAT ...
- App上架流程 & 上架被拒10大原因
上架前预热 先登陆自己的开发者账号(自己提前注册好 iOS 开发者账号,这里假设你已经拥有了一个 iOS 开发者账号),进入这个页面:https://developer.apple.com/accou ...
- 如何判断js的变量的数据类型
文章首发: http://www.cnblogs.com/sprying/p/4349426.html 本文罗列了一般的Js中类型检测的方法,实际上是每个新手在构建Js知识体系时,都要知晓的,而我只是 ...
- [Swift通天遁地]二、表格表单-(8)快速实现表单的输入验证
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Django day27 认证组件,权限组件()
一:认证组件 1.写一个类 class LoginAuth(): # 函数名一定要叫authenticate,接收必须两个参数,第二个参数是request对象 def authenticate(sel ...