• Silverlight之我见——DataGrid数据验证


    1. <UserControl x:Class="DataValidationSample.MainPage"  
    2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
    5.     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"  
    6.     xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">  
    7.   <Grid x:Name="LayoutRoot">  
    8.         <sdk:DataGrid x:Name="Grid" CanUserReorderColumns="True"  
    9.                   CanUserSortColumns="True" AutoGenerateColumns="False">  
    10.             <sdk:DataGrid.Columns>  
    11.                 <!--声明列,并进行绑定-->  
    12.                 <sdk:DataGridTextColumn  
    13.                     Header="姓名" Width="auto">  
    14.                     <sdk:DataGridTextColumn.Binding>  
    15.                         <Binding Path="Name"  
    16.                                  Mode="TwoWay"  
    17.                                  UpdateSourceTrigger="Explicit"  
    18.                                  ValidatesOnExceptions="True"  
    19.                                  NotifyOnValidationError="True"/>  
    20.                     </sdk:DataGridTextColumn.Binding>  
    21.                 </sdk:DataGridTextColumn>  
    22.                 <sdk:DataGridTextColumn  
    23.                     Header="年龄" Width="auto">  
    24.                     <sdk:DataGridTextColumn.Binding>  
    25.                         <Binding Path="Age"  
    26.                                  Mode="TwoWay"  
    27.                                  ValidatesOnExceptions="True"  
    28.                                  NotifyOnValidationError="True"  
    29.                                  UpdateSourceTrigger="Explicit"/>  
    30.                     </sdk:DataGridTextColumn.Binding>  
    31.                 </sdk:DataGridTextColumn>  
    32.                 <sdk:DataGridTextColumn  
    33.                     Header="生日" Width="auto">  
    34.                     <sdk:DataGridTextColumn.Binding>  
    35.                         <Binding Path="Birthday"  
    36.                                  Mode="TwoWay"  
    37.                                  ValidatesOnExceptions="True"  
    38.                                  NotifyOnValidationError="True"  
    39.                                  UpdateSourceTrigger="Explicit"/>  
    40.                     </sdk:DataGridTextColumn.Binding>  
    41.                 </sdk:DataGridTextColumn>  
    42.             </sdk:DataGrid.Columns>  
    43.         </sdk:DataGrid>  
    44.     </Grid>  
    45. </UserControl>  
    最后,在类用户控制的构造函数中设置数据源。
     
    1. public partial class MainPage : UserControl  
    2. {  
    3.     ObservableCollection<Employee> Employs = null;  
    4.     public MainPage()  
    5.     {  
    6.         InitializeComponent();  
    7.         this.Employs = new ObservableCollection<Employee>();  
    8.         Employs.Add(new Employee { Name = "李小同", Age = 27, Birthday = new DateTime(1988, 12, 10) });  
    9.         Employs.Add(new Employee { Name = "南郭先生", Age = 43, Birthday = new DateTime(1976, 3, 12) });  
    10.         Employs.Add(new Employee { Name = "汤老头", Age = 36, Birthday = new DateTime(1978, 5, 1) });  
    11.         Employs.Add(new Employee { Name = "林大吉", Age = 28, Birthday = new DateTime(1987, 6, 21) });  
    12.         //绑定  
    13.         this.Grid.ItemsSource = Employs;  
    14.     }  
    15. }  

    好了,请申出你的手指头,轻轻地按一下F5,把程序Run起来。
    我们在年龄上选一条记录,进入编辑状态后,输入字母(应为整数),然后试着确认,看看发生了什么事?

    在日期处也试试。

  • 相关阅读:
    Python MySQLdb 学习总结
    开始机器学习
    KMS 激活office2013失败的解决办法 Error:0xC004F038
    读研了,很不爽
    网络爬虫抓取页面的一种存储方法
    使用python自带的xml.dom创建和解析xml
    【本科毕业设计论文】分布式网络爬虫的研究与实现
    基于iptables实现NAT的分析与应用
    线程池的研究及实现
    网络爬虫中,URL队列(URL Frontier)的设计与实现
  • 原文地址:https://www.cnblogs.com/xieweikai/p/6832820.html
Copyright © 2020-2023  润新知