/// <summary>
        /// 获取DataGrid的所有行是否存在验证错误。
        /// </summary>
        /// <param name="dg">要检查的DataGrid实例</param>
        /// <returns>true 有错,false 无错</returns>
        public static bool GetDataGridRowsHasError(DataGrid dg)
        {
            bool hasError = false ;
            for (int i = 0; i < dg.Items.Count; i++)
            {
                DependencyObject o = dg.ItemContainerGenerator.ContainerFromIndex(i);
                hasError = Validation.GetHasError(o);
                if (hasError)
                {
                    break;
                }
            }
            return hasError;
        }

判断有验证错误 就不执行提交。

C# code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <summary>
        /// 获取DataGrid的第一个被发现的验证错误结果。
        /// </summary>
        /// <param name="dg">被检查的DataGrid实例。</param>
        /// <returns>错误结果。</returns>
        public static ValidationError GetDataGridRowsFirstError(DataGrid dg)
        {
            ValidationError err=null;
            for (int i = 0; i < dg.Items.Count; i++)
            {
                DependencyObject o = dg.ItemContainerGenerator.ContainerFromIndex(i);
                bool hasError = Validation.GetHasError(o);
                if (hasError)
                {
                    err = Validation.GetErrors(o)[0];
                    break;
                }
            }
            return err;
        }
 
        /// <summary>
        /// 执行检查DataGrid,并提示第一个错误。重新定位到错误单元格。
        /// </summary>
        /// <param name="dg">被检查的DataGrid实例。</param>
        /// <returns>true 有错并定位,false 无错、返回</returns>
        public static bool ExcutedCheckedDataGridValidation(DataGrid dg)
        {
            ValidationError err = GetDataGridRowsFirstError(dg);
            if (err != null)
            {
                string errColName = ((System.Windows.Data.BindingExpression)err.BindingInError).ParentBinding.Path.Path;
                DataGridColumn errCol = dg.Columns.Single(p =>
                {
                    if (((Binding)((DataGridTextColumn)p).Binding).Path.Path == errColName)
                        return true;
                    else return false;
                });
                //string errRow = ((DataRowView)((System.Windows.Data.BindingExpression)err.BindingInError).DataItem)["SWH"].ToString();
                //dg.Items.IndexOf(((System.Windows.Data.BindingExpression)err.BindingInError).DataItem);
                dg.SelectedItem = ((System.Windows.Data.BindingExpression)err.BindingInError).DataItem;
                int errRowIndex = dg.SelectedIndex;
                MessageBox.Show(string.Format("第\"{0}\"行 的\"{1}\"列的单元格数据不合法(以红色标出),请填写正确后再执行其他操作。", errRowIndex + 1, errCol.Header), "系统消息", MessageBoxButton.OK, MessageBoxImage.Warning);
 
                dg.CurrentCell = new DataGridCellInfo(dg.SelectedItem, errCol);
                if (!((DataRowView)dg.CurrentItem).IsEdit)
                {
                    ((DataRowView)dg.CurrentItem).BeginEdit();
 
                }
                TextBox txt = dg.CurrentColumn.GetCellContent(dg.CurrentItem) as TextBox;
                txt.Focus();
                return true;
            }
            else
            {
                return false;
            }
        }

获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell的更多相关文章

  1. 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列

    最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...

  2. WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴.WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用 ...

  3. WPF 4 DataGrid 控件(进阶篇一)

    原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...

  4. WPF 4 DataGrid 控件(进阶篇二)

    原文:WPF 4 DataGrid 控件(进阶篇二)      上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...

  5. WPF 4 DataGrid 控件(基本功能篇)

    原文:WPF 4 DataGrid 控件(基本功能篇)      提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...

  6. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  7. 在WPF的WebBrowser控件中抑制脚本错误

    原文:在WPF的WebBrowser控件中抑制脚本错误 今天用WPF的WebBrowser控件的时候,发现其竟然没有ScriptErrorsSuppressed属性,导致其到处乱弹脚本错误的对话框,在 ...

  8. WPF 自定义DataGrid控件样式

    内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...

  9. WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件

    之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...

随机推荐

  1. vue - .postcssrc.js

    描述:添加浏览器私缀(私缀是上世纪90年代浏览器大战的产物,也是现在新型浏览器支持某些新API,而其它浏览器不支持的证明!) 我们看看App.vue 再来看看打包后的css文件 一切都是靠你postc ...

  2. 搭建Vue环境总是出错,就重新安装就好了

    总是报错,还不如重新安装.. 错误千奇百怪,解决了 这个错误又会出现另外一个. 百度了一个挺好用的e 在window下搭建Vue.Js开发环境   nodejs官网http://nodejs.cn/下 ...

  3. 执行hbm2java工具和hbm2ddl工具

    下面内容整理自<精通hibernate> 第二版 Hibernate提供了从映射文件到Java源码的转换工具,名为hbm2java工具,能够利用ANT工具来执行它. <span st ...

  4. Navicat Premium如何打开SQL文件.MDF和.LDF文件

    相信大家再装SQL Server时都会遇到一件很头疼的事情--装完SQL server之后发现没有启动程序.没有经验的会以为SQL SERVER安装失败了于是选择重装.可是呵呵,重装了4,5次还是一样 ...

  5. iOS AVPlayer视频播放器

    代码地址如下:http://www.demodashi.com/demo/11168.html 一.运行效果 二.实现过程 ①.创建播放器avPlayer //创建播放器 url = [url str ...

  6. poj3177 Redundant Paths 边双连通分量

    给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...

  7. 年关了,抛一个模拟ip刷票的php程序

    <?php $ip = $_GET['ip'] ? $_GET['ip'] : '1.1.1.1'; $ipArr = explode(".", $ip); $ipArr[3 ...

  8. 事件总线框架---Otto

    我们假设这样一种业务场景,现在在做一款及时聊天应用,我们在聊天页面进行收发信息,同时也要实时更新前一页面的聊天记录,这时我们该如何去实现?说说我以前的实现策略,我使用的是广播接收器BroadCastR ...

  9. Linux yum操作无效的解决方法

    1.没网,试着:ping www.baidu.com 如果显示没有连接的话,就说明没网,也就无法使用yum 命令. 2.ping通了的话,还是是用不了yum命令,说明是yum镜像没有了,那么就得下载一 ...

  10. js返回页面顶部

    第一次写博客,不太专业,废话不多说,直接上自己早上做的东东.有不足之处,希望指点. css: body{counter-reset: p;} p{width: 100px;margin: 20px 0 ...