XAML Code
<Grid x:Name="LayoutRoot"> <TextBlock Height="23" HorizontalAlignment="Left" Margin="56,23,0,0" Text="用户编号:" VerticalAlignment="Top" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="56,110,0,0" Text="出生年月:" VerticalAlignment="Top" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="56,81,0,0" Text="用户年龄:" VerticalAlignment="Top" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="56,52,0,0" Text="用户名称:" VerticalAlignment="Top" /> <Rectangle Height="185" HorizontalAlignment="Left" Margin="44,9,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="351" /> <TextBox Height="23" Text="{Binding UserCode,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" HorizontalAlignment="Left" Margin="127,21,0,0" Name="txtUserCode" VerticalAlignment="Top" Width="120" /> <sdk:DescriptionViewer Margin="253,20,245,440" Height="20" Triggers="{Binding ElementName=txtUserCode}" PropertyPath="UserCode"></sdk:DescriptionViewer> <TextBox Text="{Binding UserAge,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Height="23" HorizontalAlignment="Left" Margin="127,81,0,0" Name="txtUserAge" VerticalAlignment="Top" Width="120" /> <sdk:DescriptionViewer Height="20" Margin="253,84,245,376" Triggers="{Binding ElementName=txtUserAge}" PropertyPath="UserAge"/> <TextBox Text="{Binding UserName,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Height="23" HorizontalAlignment="Left" Margin="127,52,0,0" Name="txtUserName" VerticalAlignment="Top" Width="120" /> <sdk:DescriptionViewer Height="20" Margin="253,52,245,408" Triggers="{Binding ElementName=txtUserName}" PropertyPath="UserName" /> <sdk:DatePicker Text="{Binding UserBorthDay,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Height="23" HorizontalAlignment="Left" Margin="127,110,0,0" Name="txtUserBorthDay" VerticalAlignment="Top" Width="120" /> <sdk:DescriptionViewer Height="20" Margin="253,113,245,347" Triggers="{Binding ElementName=txtUserBorthDay}" PropertyPath="UserBorthDay" /> <sdk:ValidationSummary Margin="44,137,245,288"></sdk:ValidationSummary> </Grid>
C# Code
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; 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.Navigation; namespace SLDemo3.Views { public partial class Page1 : Page { public Page1() { InitializeComponent(); this.DataContext = new UserInfo(1001, "赵大", 23, DateTime.Now); } // 当用户导航到此页面时执行。 protected override void OnNavigatedTo(NavigationEventArgs e) { } } /// <summary> /// 用户信息 /// </summary> public class UserInfo { /// <summary> /// 用户编号 /// </summary> private int _UserCode; /// <summary> /// 用户名称 /// </summary> private string _UserName; /// <summary> /// 用户年龄 /// </summary> private int _UserAge; /// <summary> /// 出生年月 /// </summary> private DateTime _UserBorthDay; public UserInfo(int usercode, string username, int userage, DateTime userborthday) { _UserCode = usercode; _UserName = username; _UserAge = userage; _UserBorthDay = userborthday; } [Display(Name = "UserCode",Description = "请输入正确的用户编号!在1000到9999之间!")] //显示 [Range(1000, 9999, ErrorMessage = "请输入正确的用户编号!在1000到9999之间!")] public int UserCode { get { return _UserCode; } set { Validator.ValidateProperty(value,new ValidationContext(this,null,null) { MemberName = "UserCode" }); _UserCode = value; } } [Display(Name = "UserName",Description = "用户名称!")] [Required(ErrorMessage = "请输入正确的用户名!")] public string UserName { get { return _UserName; } set { Validator.ValidateProperty(value,new ValidationContext(this,null,null) { MemberName = "UserName" }); _UserName = value; } } [Display(Name = "UserAge",Description = "用户年龄!在18到90之间!")] [Range(18, 90, ErrorMessage = "用户年龄!在18到90之间!")] public int UserAge { get { return _UserAge; } set { Validator.ValidateProperty(value,new ValidationContext(this,null,null) { MemberName = "UserAge" }); _UserAge = value; } } [Display(Name = "UserBorthDay", Description = "出生年月!")] public DateTime UserBorthDay { get { return _UserBorthDay; } set { Validator.ValidateProperty(value,new ValidationContext(this,null,null) { MemberName = "UserBorthDay" }); _UserBorthDay = value; } } } }
ValidationSummary 控件显示给定容器的验证错误的合并列表。
DisplayAttribute 特性应用于属性以指定用于增强这些属性值的显示的值