/// <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. iOS 调用音乐播放以及视频播放器

    音乐播放 NSString *path = [[NSBundle mainBundle] pathForResource:@"预谋" ofType:@"mp3" ...

  2. bin和sbin区别

    据说这个目录结构是沿袭unix的,不大清楚. bin是binary的缩写,是可执行的二进制文件./bin里面一般是基本的,大家都要用的工具:sbin里面的s是system的意思,是供system ad ...

  3. Android Exception 9(requestFeature() must be called before adding content)

    08-05 17:36:12.799: W/System.err(10378): java.lang.reflect.InvocationTargetException08-05 17:36:12.7 ...

  4. 【DB2】If 'db2' is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found

    数据库安装以后,db2报错如下: If 'db2' is not a typo you can run the following command to lookup the package that ...

  5. 05-spring-bean注入

    spring中只有两大核心技术: 控制反转(IOC)&依赖注入(DI),AOP(面向切面编程) 依赖注入:指利用配置文件的关系,来决定类之间的引用关系,以及数据的设置操作. 构造方法注入 默认 ...

  6. spring日志加载代码解析

    项目用的是springmvc+spring+mybatis框架, 配置日志的时候非常简单,仅仅是把commons-logging.log4j,还有slf4j-log4j三个日志相关的jar包导入项目, ...

  7. 从MVC和三层架构说到ssh整合开发-下

    这章主要讲整合开发,直接从实战讲起,对与ssh的单方面了解,请继续等待我的兴许文章. 解说不到位的地方欢迎大家指正:联系方式rlovep.com 具体请看源码凝视: 全部代码下载(csdn):链接 G ...

  8. 通过 thread dump 分析找到高CPU耗用与内存溢出的Java代码

    http://heylinux.com/archives/1085.html通过 thread dump 分析找到高CPU耗用与内存溢出的Java代码 首先,要感谢我的好朋友 钊花 的经验分享. 相信 ...

  9. C#实现冲顶大会辅助工具(截图+图像识别+搜索)

    前两天在博客园看到 .NET开发一个微信跳一跳辅助程序, 原来可以通过C#连接手机操作.正好朋友圈有人分享“冲顶大会”.冲顶大会是一个在线答题APP.每次12道题,每道题有10秒钟的答题时间,全对者瓜 ...

  10. xpath的基础实例

    本文分为路径表达式和常用函数两部分,整理自火车浏览器官方教程-火车浏览器之Xpath讲解. 小提示:可以使用火狐浏览器.我用的是火狐浏览器+firebug+firepath来进行调试,调试界面是这样的 ...