Silverlight之我见——DataGrid数据验证

Silverlight之我见——DataGrid数据验证,第1张

概述  用DataGrid进行数据验证,一句话:太简单了,为什么?因为它自身已经具备了很完善的数据验证功能。 好,说多无益,最好的办法,来,写个例子试试。 老规矩,先准备点数据来测试,既然要数据验证,就不能全弄字符串的,弄点整型的,日期型的,这样就有利于演示。   public class Employee { public string Name { get; set; }   用DataGrID进行数据验证,一句话:太简单了,为什么?因为它自身已经具备了很完善的数据验证功能。 好,说多无益,最好的办法,来,写个例子试试。 老规矩,先准备点数据来测试,既然要数据验证,就不能全弄字符串的,弄点整型的,日期型的,这样就有利于演示。  
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.@[email protected];assembly=System.@[email protected]">  <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数据验证所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存