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中属性间存在的关系,我们需要编写验证规则如下:
- //自定义验证规则
- public class StudentValidationRule:ValidationRule
- {
- public static string errormessage = string.Empty;
- public override ValidationResult Validate(object value,
- System.Globalization.CultureInfo cultureInfo)
- {
- errormessage = "";
- if (value is BindingGroup)
- {
- BindingGroup group = (BindingGroup)value;
- foreach (var item in group.Items)
- {
- student st = item as student;
- if (string.IsNullOrEmpty(st.Name.Trim()) || string.IsNullOrEmpty(st.Sex.Trim())
- || string.IsNullOrEmpty(st.Num.Trim()))
- {
- errormessage = "姓名、性别、成绩都不能为空!";
- return new ValidationResult(false, "姓名、性别、成绩都不能为空!");
- }
- //Num为A或B则通过,为C则为不通过。
- if ((st.Num.Equals("C") && st.Chek) || ((st.Num.Equals("A") || st.Num.Equals("B")) && !st.Chek))
- {
- errormessage = "分数等级与是否通过不符!";
- return new ValidationResult(false, "分数等级与是否通过不符!");
- }
- }
- }
- return ValidationResult.ValidResult;
- }
- }
其中BindingGroup包含用于验证对象绑定和ValidationRule 对象的集合.当我们定义好自己的验证时,我们需要将规则部署到DataGrid上,前台部署:
- <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"
- RowStyle="{StaticResource RowStyle}" SelectionChanged="dg_SelectionChanged" RowEditEnding="dg_RowEditEnding">
- <DataGrid.RowValidationRules>
- <local:StudentValidationRule ValidationStep="UpdatedValue"/>
- </DataGrid.RowValidationRules>
- <DataGrid.Columns>
- <DataGridTextColumn Binding="{Binding Path=Name}" Header="姓名" Width="100"/>
- <DataGridTextColumn Binding="{Binding Path=Sex}" Header="性别" Width="100"/>
- <DataGridTemplateColumn Header="分数" >
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Path=Num}"/>
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- <DataGridTemplateColumn.CellEditingTemplate>
- <DataTemplate>
- <ComboBox x:Name="taskcob"
- SelectedValue="{Binding Num}"
- ItemsSource="{Binding Type,Source={StaticResource So} }"
- />
- </DataTemplate>
- </DataGridTemplateColumn.CellEditingTemplate>
- </DataGridTemplateColumn>
- <DataGridCheckBoxColumn Binding="{Binding Path=Chek}" Header="是否通过" Width="*"/>
- </DataGrid.Columns>
- </DataGrid>
如上所示,DataGrid.RowValidationRules(本身就是个BindingGroup)给该Datagrid指定了验证的规则,ValidationStep则设定在何时触发该验证。DataGrid中我们对RowStyle属性进行了设定;也就是当验证未通过时,系统会反馈到界面,提醒用户哪里出错了;对于样式的编写,多种多样,大家可根据自己的喜好编写样式。当datagrid中出现验证错误时,系统将会把编辑行锁定为出错行,此时其他行是禁止被编辑的,直到该行中的验证全部通过为止。
运行如下:

WPF中DATAGRID自定义验证(包含BINDINGGROUP)的更多相关文章
- WPF中DataGrid自定义实现最后一行下面跟一个汇总行,类似MT4
1.先看MT4实现的效果:(图中红框部分),其实就是DataGrid在最后一行下面跟一个汇总的显示条 2.看我WPF实现的效果,汇总行中的数据可以绑定哦!效果图如下: 我扩展了一下DataGrid控件 ...
- WPF中的数据验证
数据验证 WPF的Binding使得数据能够在数据源和目标之间流通,在数据流通的中间,便能够对数据做一些处理. 数据转换和数据验证便是在数据从源到目标 or 从目标到源 的时候对数据的验证和转换. V ...
- WPF中实现自定义虚拟容器(实现VirtualizingPanel)
WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...
- WPF中datagrid不显示滚动条问题
WPF中DataGrid是自带滚动条的,当内容显示不下时可以使用滚动条显示.但是,开发中遇到了DataGrid一直不显示滚动条.之前也曾遇到过同类问题,再次经过查找,发现是StackPanel的原因, ...
- WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)
原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...
- Working Experience - WPF 中 DataGrid 控件的应用
问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...
- asp.net mvc 中的自定义验证(Custom Validation Attribute)
前言
- C# wpf中DataGrid 支持汇总行
最近有一个需求,需要汇总金额,份额等字段.我们使用的是原生的WPF控件,自己开发了一套Template.而没有使用比较成熟的第三方控件.所以这个功能得自己开发.并且要做成控件层次的功能. 当然也可以这 ...
- wpf 中DataGrid 控件的样式设置及使用
本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </ ...
随机推荐
- 527. Word Abbreviation
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- LeetCode OJ-- Interleaving String **@
https://oj.leetcode.com/problems/interleaving-string/ 刚开始用递归做,但是超时了 class Solution { public: bool fl ...
- 关于WEB页面的强制分页打印问题
最近项目中有个需求要求打印web页面数据,但是碰到打印预览显示数据时,多的数据就不翼而飞了(不分页),搞的很是焦灼~ 最先是以为纸张的问题,胡乱折腾了一番,把A4约硬是改成了LARGE号的纸,多的数据 ...
- ACM的奇计淫巧_扩栈C++/G++
C++ #pragma comment(linker, "/STACK:102400000,102400000") G++ << ; // 256MB char *p ...
- Codeforces 583 DIV2 Asphalting Roads 模拟
原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...
- linux查看hostname以及修改hostname
查看hostname : hostname 修改hostname : hostnamectl set-hostname master (比如要修改为master) 修改完重启生效 : ...
- Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
这一篇文章是继http://www.cnblogs.com/EasonJim/p/5954155.html的升级版,由于CCNET已经过时,所以我把打包过程的CCNET工具换成Jenkins去实现,批 ...
- Oracle Forms Project For Students Free Download
After getting to much requests for a free Oracle Forms and Reports project for students, I am sharin ...
- Android Spinner In Toolbar
As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...
- 【spring cloud】注解@SpringCloudApplication和@SpringBootApplication的区别
@SpringCloudApplication和@SpringBootApplication的区别