Silverlight使用数据批注自定义数据类
当您在 Silverlight 应用程序中使用数据类时,您可以将属性应用于指定验证规则的类或成员,指定显示数据的方式以及设置实体类之间的关系。System.ComponentModel.DataAnnotations 命名空间包含用作数据属性的类。通过对数据类或成员应用这些属性,您可以集中处理数据定义,而不必在多个位置重新应用相同的规则。
数据批注属性分为三类:验证属性、显示属性和数据建模属性。本主题介绍这些属性并提供示例。
验证属性 System.ComponentModel.DataAnnotations 命名空间包含以下属性,它们用于对应用于类或成员的数据执行验证规则:
|
验证属性 |
说明 |
|---|---|
|
使用自定义方法进行验证。 | |
|
指定特定类型的数据,如电子邮件地址或电话号码。 | |
|
确保值存在于枚举中。 | |
|
指定最小和最大约束。 | |
|
使用正则表达式来确定有效的值。 | |
|
指定必须提供一个值。 | |
|
指定最大和最小字符数。 | |
|
用作验证属性的基类。 |
所有验证属性均派生自 ValidationAttribute 类。确定值是否有效的逻辑在重写的 IsValid 方法中实现。Validate 方法将调用 IsValid 方法,如果值无效,则引发 ValidationException。
要创建自定义的验证检查,您可以创建派生自 ValidationAttribute 类的类,或创建一个执行验证检查的方法,并在将 CustomValidationAttribute 应用于数据成员时引用该方法。当创建派生自 ValidationAttribute 的类时,重写 IsValid 方法以便为您的自定义验证检查提供逻辑。
目前,DataGrid 控件是唯一自动应用验证属性的控件。有关将 DataGrid 控件用于包含验证属性的类的示例,请参见下面的"数据网格示例"。当您不使用 DataGrid 控件时,必须手动验证这些值。
手动验证值
当您不使用 DataGrid 控件提供用于编辑数据的接口时,将不自动应用验证属性。然而,您可以使用 Validator 类手动应用验证测试。您可以对某属性 (Property) 的 set 访问器调用 ValidateProperty 方法,以针对该属性 (Property) 的验证属性 (Attribute) 检查值。当数据绑定从验证属性 (Attribute) 中收到验证异常时,您还必须同时将 ValidatesOnExceptions 和 NotifyOnValidationError 属性 (Property) 设置为 true。有关手动应用验证的示例,请参见下面的"数据绑定示例"。
显示属性 System.ComponentModel.DataAnnotations 命名空间包含以下属性,它们用于指定如何显示类或成员中的数据:
|
显示属性 |
说明 |
|---|---|
|
指定特定类型的数据,如电子邮件地址或电话号码。 | |
|
为用户界面中使用的数据类型和成员指定可本地化的字符串。 | |
|
当将表用作外键关系中的父表时,指定显示和排序属性。 | |
|
指定如何显示数据字段以及如何设置数据字段的格式。 | |
|
指定列的筛选行为。 | |
|
指定用于显示关联实体成员的控件和值。 |
显示属性在与 DataGrid 控件一起使用时将自动应用。当使用诸如 Label 和 DescriptionViewer 的控件进行数据绑定时,可以手动检索显示属性值。有关将 Label 和 DescriptionViewer 控件用于显示属性的示例,请参见下面的"数据绑定示例"。
DataTypeAttribute 属性指定特定类型的数据成员,因此可同时用作验证属性和显示属性。
数据建模属性 System.ComponentModel.DataAnnotations 命名空间包含以下属性,它们用于指定数据成员的目标用途以及数据类之间的关系:
|
数据建模属性 |
说明 |
|---|---|
|
指定某个实体成员表示某种数据关系,如外键关系。 | |
|
指定某属性将参与开放式并发检查。 | |
|
指定用户是否应能够更改实体属性的值。 | |
|
指定一个或多个要用作实体的唯一标识的实体属性。 | |
|
将某个成员指定为数据版本控制的时间戳值。 |
数据网格示例 本示例说明一个 DataGrid 控件,用户通过该控件可以查看或编辑数据类中的数据。数据批注应用于数据类,以指定显示和验证行为。验证和显示属性在与 DataGrid 控件一起使用时将自动应用。
下面的示例说明名为 Product 的数据类以及应用于成员的数据批注。
public class Product { [Display(Name="Product Number")] [Range(0, 5000)] public int ProductID { get; set; } [Display(Name="Name")] [Required] public string ProductName { get; set; } [Display(Name="Price")] [DataType(DataType.Currency)] public double ListPrice { get; set; } [EnumDataType(typeof(ProductColor))] public ProductColor Color { get; set; } [Display(Name="Available")] public bool InStock { get; set; } public Product() { } public Product(int _productID, string _productName, double _listPrice, ProductColor _color, bool _inStock) { this.ProductID = _productID; this.ProductName = _productName; this.ListPrice = _listPrice; this.Color = _color; this.InStock = _inStock; } } public enum ProductColor { Red, White, Purple, Blue }
下面的示例演示用于在数据网格中显示数据类的页面和代码隐藏文件。
public partial class ProductPage : Page { ObservableCollection<Product> AvailableProducts; public ProductPage() { InitializeComponent(); AvailableProducts = new ObservableCollection<Product>(); AvailableProducts.Add(new Product(1, "Bike", 500, ProductColor.Red, true)); AvailableProducts.Add(new Product(2, "Chair", 250, ProductColor.White, true)); AvailableProducts.Add(new Product(3, "Plate", 20, ProductColor.Purple, false)); AvailableProducts.Add(new Product(4, "Kite", 15, ProductColor.Blue, true)); DataGrid1.ItemsSource = AvailableProducts; } }
数据绑定示例 此示例演示一个 ValidationSummary 控件,该控件在一个表单中显示三个 TextBox 控件的错误。验证规则在 Customer 类中的各个属性内指定,并且手动应用于 set 访问器中。ValidatesOnExceptions 和 NotifyOnValidationError 属性在数据绑定中设置为 true。
using System.ComponentModel.DataAnnotations; using System.Windows.Controls; namespace ValidationSample { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.DataContext = new Customer("J", "Smith", 12345); } } public class Customer { // Private data members. private int m_IdNumber; private string m_FirstName; private string m_LastName; public Customer(string firstName, string lastName, int id) { this.IdNumber = id; this.FirstName = firstName; this.LastName = lastName; } // Public properties. [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999.")] [Range(0, 99999)] public int IdNumber { get { return m_IdNumber; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "IdNumber" }); m_IdNumber = value; } } [Display(Name="Name", Description="First Name + Last Name.")] [Required(ErrorMessage = "First Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] public string FirstName { get { return m_FirstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FirstName" }); m_FirstName = value; } } [Required(ErrorMessage = "Last Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] [StringLength(8, MinimumLength = 3, ErrorMessage = "Last name must be between 3 and 8 characters long.")] public string LastName { get { return m_LastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); m_LastName = value; } } } }

浙公网安备 33010602011771号