cobra - 2009-9-11 12:27:00
上一篇:
SilverLight学习笔记--实际应用(一)(3):手把手建立一个Silverlight应用程序之删除记录现在我们的程序有了添加和删除以及修改功能,下面我们看一看如何让程序具备数据校验功能。我们将用两种方式实现数据的校验,一种是在客户端进行同步校验。另一种是在服务器端进行异步校验。
本篇我们先实现如何在客户端进行同步校验。基本原理是:利用数据的双向绑定和INotifyPropertyChanged 接口以及控件的NotifyOnValidationError与ValidatesOnExceptions两个属性设置来配合实现客户端同步校验。
现在继续修改完善我们的程序。
1、修改用户界面如下:- <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SLApplicationDataTest.Page"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Width="400" Height="300">
- <StackPanel Height="500" Background="White">
- <StackPanel Orientation="Horizontal">
- <Button x:Name="addButton" Content="Add" Margin="10"/>
- <Button x:Name="deleteButton" Content="Delete" Margin="10"/>
- </StackPanel>
- <data:DataGrid x:Name="dgPeople" AutoGenerateColumns="False" >
- <data:DataGrid.Columns>
- <data:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
- <data:DataGridTextColumn Header="Sex" Binding="{Binding Sex}" />
- <data:DataGridTemplateColumn Header="Age">
- <data:DataGridTemplateColumn.CellTemplate >
- <DataTemplate>
- <TextBlock Text="{Binding Age}" ToolTipService.ToolTip="请输入0至200之间的整数!"></TextBlock>
- </DataTemplate>
- </data:DataGridTemplateColumn.CellTemplate>
- <data:DataGridTemplateColumn.CellEditingTemplate>
- <DataTemplate>
- <TextBox Text="{Binding Age , Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" BindingValidationError="TextBox_BindingValidationError"
- ToolTipService.ToolTip="请输入0至200之间的整数!"></TextBox>
- </DataTemplate>
- </data:DataGridTemplateColumn.CellEditingTemplate>
- </data:DataGridTemplateColumn>
- <data:DataGridTextColumn Header="Address" Binding="{Binding Address}" />
- </data:DataGrid.Columns>
- </data:DataGrid>
- </StackPanel>
- </UserControl>
复制代码在此界面中,我们不再是简单地进行绑定数据和显示数据,而是对DataGrid控件进行了改造,尤其是运用了模板来定义我们的Age字段。注意Age字段的CellEditingTemplate模板,它的两个属性:NotifyOnValidationError,ValidatesOnExceptions都必须设置为true,这样才会在绑定出错时报错,而对错误事件的处理则字义在BindingValidationError事件中。
Silverlight栏目的最新浏览文章:
•
通过动态构造实体在Silverlight 中给DataGrid 绑定数据 •
Silverlight基于WCF双工以及.NET TCP协议即时通讯系统 •
Silverlight 4 离线简体中文版帮助文档 •
使用Silverlight构建一个工作流设计器(十六)-持久化数据到数据库—数据库结构 •
最新Microsoft Expression Studio 3破解 •
微软Expression Blend 3中文教程 •
【Silverlight】利用IsolatedStorageFile实现客户端缓存 •
Silverlight 的弹出窗体--展示 •
Silverlight 4中使用MEF实现页面的动态装配 •
Silverlight实现360度全景展示效果 •
Pro Business Applications with Silverlight 4 •
Silverlight实时3D渲染引擎:SilverMotion •
Silverlight中把WriteableBitmap转为Byte流并保存到本地 •
Silverlight 图片局部放大效果 •
Silverlight 4 User Interface Cookbook •
Silverlight 相册DEMO--ImageSnipper(V2)
cobra - 2009-9-11 12:27:00
2、修改Person类 在此类中加入绑定数据的校验工作。具体代码如下:
- Public int Age
- {
- get { return _age; }
- set
- {
- if (value == _age) return;
- //如果输入的不是整数,则抛出异常
- try
- {
- Convert.ToInt32(value);
- }
- catch(Exception ex)
- {
- throw new Exception(ex.ToString());
- }
-
- //如果输入的整数不在合理范围同,则也抛出异常
- if (value < 0 || value > 200)
- {
- throw new Exception("Age must be between 0 and 200");
- }
- _age = value;
- OnPropertyChanged("Age");
- }
- }
复制代码Person类全部代码如下:
cobra - 2009-9-11 12:27:00
3、完成校验错误处理程序- #region 校验错误处理程序
- private void TextBox_BindingValidationError(object sender, ValidationErrorEventArgs e)
- {
-
- if (e.Action == ValidationErrorEventAction.Added)
- {
- //((Control)e.OriginalSource).Background = new SolidColorBrush(Colors.Red);
- //((Control)e.OriginalSource).SetValue(ToolTipService.ToolTipProperty, e.Error.Exception.Message);
- //((Control)e.OriginalSource).Focus();
- this.Dispatcher.BeginInvoke(() => HtmlPage.Window.Alert(e.Error.Exception.Message));
-
- }
- else if (e.Action == ValidationErrorEventAction.Removed)
- {
- //((Control)e.OriginalSource).Background = new SolidColorBrush(Colors.White);
- //((Control)e.OriginalSource).SetValue(ToolTipService.ToolTipProperty, null);
- }
- }
- #endregion
复制代码Page.xaml.cs全部代码如下:
运行效果如下:
附件:
1.png 附件:
2.png 文/wsdj