WPF中DATAGRID自定义验证(包含BINDINGGROUP)

DataGrid在Wpf中的应用是十分广泛的,当你需要表中的信息稍详细的显示出来时,或者我们需要进行某些数据输入时,都有可能采用DataGrid。当然对信息的显示,我们不需要进行验证,但当我们将DataGrid作为输入工具时,输入的数据要符合相应的规则后才能通过,这时我们就需呀进行验证了。
   对于DataGrid的验证有两种,一种是对每个DataGridCell而言的,也就是说当每个被验证的Cell编辑完成后,就会触发验证;另一种是针对DataGridRow而言的,就是当整行编辑完成之后触发验证。两者的选择就要根据具体的情况而定了。
   对于验证的实现,一般也分为两种方法,一种是实现IDataErrorInfo接口来完成验证,另一种则是需要继承ValidationRule来完成自定义的验证。本文主要针对后者进行说明。随后也会附上DEMO。
   我们在WPF页中用DataGrid对多有的学生Student信息进行显示,但更重要的是我们要在DataGrid中对学生列表进行新增或编辑。
   其中Student类中定义了Name、Sex、Num、Check等属性,对每个Student的每个属性我们要求都不为空,对于这点,我们用整行验证和各个Cell进行验证都是可以的,但是对于Num和check我们有要求:Num为分数等级,只有当它为A或B的时候Check才为true(表通过),这时用整行的验证做起来比较简单,也就是说当验证每个实体属性间关系时用整行验证来说比较方便。
   要验student中属性间存在的关系,我们需要编写验证规则如下:
[csharp] view plain copy
 
  1. //自定义验证规则  
  2.     public class StudentValidationRule:ValidationRule  
  3.     {  
  4.         public static string errormessage = string.Empty;  
  5.         public override ValidationResult Validate(object value,  
  6.             System.Globalization.CultureInfo cultureInfo)  
  7.         {  
  8.             errormessage = "";   
  9.             if (value is BindingGroup)  
  10.             {  
  11.                 BindingGroup group = (BindingGroup)value;  
  12.                 foreach (var item in group.Items)  
  13.                 {  
  14.                     student st = item as student;  
  15.                     if (string.IsNullOrEmpty(st.Name.Trim()) || string.IsNullOrEmpty(st.Sex.Trim())  
  16.                         || string.IsNullOrEmpty(st.Num.Trim()))  
  17.                     {  
  18.                         errormessage = "姓名、性别、成绩都不能为空!";  
  19.                         return new ValidationResult(false, "姓名、性别、成绩都不能为空!");  
  20.                     }  
  21.                     //Num为A或B则通过,为C则为不通过。  
  22.                     if ((st.Num.Equals("C") && st.Chek) || ((st.Num.Equals("A") || st.Num.Equals("B")) && !st.Chek))  
  23.                     {  
  24.                         errormessage = "分数等级与是否通过不符!";  
  25.                         return new ValidationResult(false, "分数等级与是否通过不符!");  
  26.                     }  
  27.                 }  
  28.             }  
  29.             return ValidationResult.ValidResult;  
  30.         }  
  31.   
  32.     }   

       其中BindingGroup包含用于验证对象绑定和ValidationRule 对象的集合.当我们定义好自己的验证时,我们需要将规则部署到DataGrid上,前台部署:

[csharp] view plain copy
 
  1. <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="True" Canvas.Top="0" Name="dg" Width="500" IsReadOnly="False"  BeginningEdit="dg_BeginningEdit" CellEditEnding="dg_CellEditEnding" LostFocus="dg_LostFocus" SelectedCellsChanged="dg_SelectedCellsChanged"  
  2.                       RowStyle="{StaticResource RowStyle}" SelectionChanged="dg_SelectionChanged" RowEditEnding="dg_RowEditEnding">  
  3.                 <DataGrid.RowValidationRules>  
  4.                     <local:StudentValidationRule ValidationStep="UpdatedValue"/>  
  5.                 </DataGrid.RowValidationRules>  
  6.                 <DataGrid.Columns>  
  7.                     <DataGridTextColumn Binding="{Binding Path=Name}" Header="姓名" Width="100"/>  
  8.                     <DataGridTextColumn Binding="{Binding Path=Sex}" Header="性别" Width="100"/>  
  9.                     <DataGridTemplateColumn Header="分数" >  
  10.                         <DataGridTemplateColumn.CellTemplate>  
  11.                             <DataTemplate>  
  12.                                 <TextBlock Text="{Binding Path=Num}"/>  
  13.                             </DataTemplate>  
  14.                         </DataGridTemplateColumn.CellTemplate>  
  15.                         <DataGridTemplateColumn.CellEditingTemplate>  
  16.                             <DataTemplate>  
  17.                                 <ComboBox x:Name="taskcob"  
  18.                                           SelectedValue="{Binding Num}"  
  19.                                           ItemsSource="{Binding Type,Source={StaticResource So} }"  
  20.                                           />  
  21.                             </DataTemplate>  
  22.                         </DataGridTemplateColumn.CellEditingTemplate>  
  23.                     </DataGridTemplateColumn>  
  24.                     <DataGridCheckBoxColumn Binding="{Binding Path=Chek}" Header="是否通过" Width="*"/>  
  25.                 </DataGrid.Columns>  
  26.             </DataGrid>  

     如上所示,DataGrid.RowValidationRules(本身就是个BindingGroup)给该Datagrid指定了验证的规则,ValidationStep则设定在何时触发该验证。DataGrid中我们对RowStyle属性进行了设定;也就是当验证未通过时,系统会反馈到界面,提醒用户哪里出错了;对于样式的编写,多种多样,大家可根据自己的喜好编写样式。当datagrid中出现验证错误时,系统将会把编辑行锁定为出错行,此时其他行是禁止被编辑的,直到该行中的验证全部通过为止。

    运行如下:

 

http://blog.csdn.net/zhanglikeno1/article/details/8718096

 
 
原文地址:https://www.cnblogs.com/sjqq/p/8028217.html