silverlight:强大的DataGrid组件[14][Final]_数据验证——Silverlight学习笔记[22]来源: 发布时间:星期五, 2009年9月25日 浏览:0次 评论:0
转载自:http://www.cnblogs.com/kinglee/archive/2009/09/01/1558314.html
数据验证是DataGrid应用中的重要一环。正确的数据能保证应用程序的有效。根据微软提供的资料显示,DataGrid是唯一具有自动验证数据能力的数据控件(根据业务对象的属性类型自动做出判断)。然而,这一自动验证所提供的功能仅仅是基本的。在实际应用中,我们往往需要特定格式的数据验证。本文将为大家介绍如何在DataGrid中实施数据验证。 具体步骤 1)建立业务对象 2)为对象中各成员属性通过metadata标签进行数据的验证 3)如果需要进行自定义数据验证,则需另行创建自定义验证类。 几个用于数据验证的metadata的说明 1)[Required]:表示所验证数据必须非空。 2)[RegularExpression]:用正则表达式验证数据。 3)[Range]:对于被验证数据的最值范围进行设定。 4)[CustomValidation]:用于自定义验证数据的设置。 实例 详细的说明在代码中给出。 注意:用VS调试时,要在菜单Debug->Exception...,去掉Common Language Runtime Exception前面的勾,不然会报错。 1)业务类Employees.cs文件: using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace SilverlightClient { public class Employees: INotifyPropertyChanged { private int _EmployeeID; private string _EmployeeName; private int _EmployeeAge; private string _EmployeeTelephone; [Required(ErrorMessage="该字段为必填!")] [CustomValidation(typeof(EmployeeValidation), "IsValidID")]//这里用到了自定义验证,自定义验证类为EmployeeValidation.cs public int EmployeeID { get { return _EmployeeID; } set { if (value != _EmployeeID) { _EmployeeID = value; OnPropertyChanged("EmployeeID"); } } } [Required(ErrorMessage = "该字段为必填!")] public string EmployeeName { get { return _EmployeeName; } set { if (value != _EmployeeName) { _EmployeeName = value; OnPropertyChanged("EmployeeName"); } } } [Required(ErrorMessage = "该字段为必填!")] [Range(18,60,ErrorMessage = "输入的年龄范围有误(18-60)!\n请重新填写。")] public int EmployeeAge { get { return _EmployeeAge; } set { if (value != _EmployeeAge) { _EmployeeAge = value; OnPropertyChanged("EmployeeAge"); } } } [Required(ErrorMessage = "该字段为必填!")] [RegularExpression(@"^\(\d\d\d\) \d\d\d\d\d\d\d\d$",ErrorMessage = "输入的电话格式不正确!\n请按(###) ########格式匹配输入。")] public string EmployeeTelephone { get { return _EmployeeTelephone; } set { if (value != _EmployeeTelephone) { //用VS调试时,要在菜单Debug->Exception...,去掉Common Language Runtime Exception前面的勾,不然会报错。 Validator.ValidateProperty(value, new ValidationContext(this,null,null) { MemberName = "EmployeeTelephone" });//跳出提示框显示出错原因 _EmployeeTelephone = value; OnPropertyChanged("EmployeeTelephone"); } } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } #endregion } } 2)自定义验证类EmployeeValidation.cs文件: using System.ComponentModel.DataAnnotations; namespace SilverlightClient { public class EmployeeValidation { public static ValidationResult IsValidID(int employeeid) { bool isValid; if (employeeid > 0) isValid = true; else isValid = false; if (isValid) { return ValidationResult.Success; } else { return new ValidationResult("输入的工号无效!"); } } } } 3)MainPage.xaml文件 <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage" d:DesignWidth="320" d:DesignHeight="240"> <Grid x:Name="LayoutRoot" Background="White" Width="320" Height="240"> <data:DataGrid x:Name="dgEmployee" Margin="8" AutoGenerateColumns="False" FontSize="14"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="工号" Binding="{Binding EmployeeID}"/> <data:DataGridTextColumn Header="姓名" Binding="{Binding EmployeeName}"/> <data:DataGridTextColumn Header="年龄" Binding="{Binding EmployeeAge}"/> <data:DataGridTextColumn Header="电话" Binding="{Binding EmployeeTelephone}"/> </data:DataGrid.Columns> </data:DataGrid> </Grid> </UserControl> 4)MainPage.xaml.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Data; namespace SilverlightClient { public partial class MainPage : UserControl { public List<Employees> GetEmployees() { List<Employees> returnedValue = new List<Employees>(); returnedValue.Add(new Employees() { EmployeeID = 1, EmployeeName = "张三", EmployeeAge = 23, EmployeeTelephone = "(001) 56891243" }); returnedValue.Add(new Employees() { EmployeeID = 2, EmployeeName = "李四", EmployeeAge = 24, EmployeeTelephone = "(002) 67453210" }); returnedValue.Add(new Employees() { EmployeeID = 3, EmployeeName = "王五", EmployeeAge = 25, EmployeeTelephone = "(010) 46782312" }); returnedValue.Add(new Employees() { EmployeeID = 4, EmployeeName = "赵六", EmployeeAge = 26, EmployeeTelephone = "(011) 78564254" }); returnedValue.Add(new Employees() { EmployeeID = 5, EmployeeName = "钱七", EmployeeAge = 27, EmployeeTelephone = "(020) 87465676" }); returnedValue.Add(new Employees() { EmployeeID = 6, EmployeeName = "孙八", EmployeeAge = 28, EmployeeTelephone = "(030) 97321687" }); return returnedValue; } public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { dgEmployee.ItemsSource = GetEmployees(); } } } 最终效果图 作者:Kinglee
文章出处:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/) 版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。 0
相关文章
读者评论发表评论 |
|