public class Employee { public string name { get; set; } public int Age { get; set; } public DateTime Birthday { get; set; } }
该类声明三个属性,分别是字符串,整型,日期型。 好,我们来看看DataGrID默认的验证功能。 要进行验证只需做好以下工作: 把Binding的ValIDatesOnExceptions设为True,NotifyOnValIDationError设为true,UpdateSourceTrigger设置为Explicit。 好,看XAML:
<UserControl x:Class="DataValIDationSample.MainPage" 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" d:DesignWIDth="640" d:DesignHeight="480" xmlns:sdk="clr-namespace:System.@R_301_5087@.Controls;assembly=System.@R_301_5087@.Controls.Data"> <GrID x:name="LayoutRoot"> <sdk:DataGrID x:name="GrID" CanUserReorderColumns="True" CanUserSortColumns="True" autoGenerateColumns="False"> <sdk:DataGrID.Columns> <!--声明列,并进行绑定--> <sdk:DataGrIDTextColumn header="姓名" WIDth="auto"> <sdk:DataGrIDTextColumn.Binding> <Binding Path="name" Mode="TwoWay" UpdateSourceTrigger="Explicit" ValIDatesOnExceptions="True" NotifyOnValIDationError="True"/> </sdk:DataGrIDTextColumn.Binding> </sdk:DataGrIDTextColumn> <sdk:DataGrIDTextColumn header="年龄" WIDth="auto"> <sdk:DataGrIDTextColumn.Binding> <Binding Path="Age" Mode="TwoWay" ValIDatesOnExceptions="True" NotifyOnValIDationError="True" UpdateSourceTrigger="Explicit"/> </sdk:DataGrIDTextColumn.Binding> </sdk:DataGrIDTextColumn> <sdk:DataGrIDTextColumn header="生日" WIDth="auto"> <sdk:DataGrIDTextColumn.Binding> <Binding Path="Birthday" Mode="TwoWay" ValIDatesOnExceptions="True" NotifyOnValIDationError="True" UpdateSourceTrigger="Explicit"/> </sdk:DataGrIDTextColumn.Binding> </sdk:DataGrIDTextColumn> </sdk:DataGrID.Columns> </sdk:DataGrID> </GrID></UserControl>
最后,在类用户控制的构造函数中设置数据源。
public partial class MainPage : UserControl { ObservableCollection<Employee> Employs = null; public MainPage() { InitializeComponent(); this.Employs = new ObservableCollection<Employee>(); Employs.Add(new Employee { name = "李小同",Age = 27,Birthday = new DateTime(1988,12,10) }); Employs.Add(new Employee { name = "南郭先生",Age = 43,Birthday = new DateTime(1976,3,12) }); Employs.Add(new Employee { name = "汤老头",Age = 36,Birthday = new DateTime(1978,5,1) }); Employs.Add(new Employee { name = "林大吉",Age = 28,Birthday = new DateTime(1987,6,21) }); //绑定 this.GrID.ItemsSource = Employs; } }
好了,请申出你的手指头,轻轻地按一下F5,把程序Run起来。 我们在年龄上选一条记录,进入编辑状态后,输入字母(应为整数),然后试着确认,看看发生了什么事?
在日期处也试试。
原文链接: http://www.voidcn.com/article/p-hqmgvgwb-bgd.html 总结
以上是内存溢出为你收集整理的Silverlight之我见——DataGrid数据验证全部内容,希望文章能够帮你解决Silverlight之我见——DataGrid数据验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)