/// <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 - static(.gitkeep)

    描述:git上传忽略的文件,与.gitnoignore一样.

  2. selenium-Navigating

    The first thing you’ll want to do with WebDriver is navigate to a link. The normal way to do this is ...

  3. proxyTable 解决跨域问题

    1.使用 proxyTable(地址映射表)解决跨域问题(即通过设置代理解决跨域问题): 可以通过设置将复杂的url简化,例如我们要请求的地址是api.xxxxxxxx.com/list/1,可以按照 ...

  4. Android使用SeekBar

    转自:http://my.oschina.net/pingdy/blog/376735 最近有做一个android项目,里面有使用到在播放视频时可以跳播,同时动态显示播放时间.类似于下图 的效果,我只 ...

  5. 08-hibernate注解-多对多单向外键关联

    多对多单向外键 1,学生和教师构成多对多的关联关系 2,其中一个多方持有另一个多方的集合对象(学生持有教室的集合) 3,通过注解@JoinTable,创建中间表(作为多对多的载体,用来确定学生和教师的 ...

  6. Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享

    Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享 2014-09-09 14:14:25 标签:会话共享 主从 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...

  7. swift向方法传数组参数的语法

    总是记不住向方法中传数组参数的语法,所以记录一下. func calculateStatistics(scores:[Int]) -> (min:Int,max:Int,sum:Int) { v ...

  8. css定位positon

    值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", " ...

  9. DS18B20 crc 算法

    http://blog.csdn.net/pengrui18/article/details/24740973 https://www.maximintegrated.com/cn/app-notes ...

  10. Application Request Route实现IIS Server Farms集群负载详解(转)

    http://www.cnblogs.com/knowledgesea/p/5099893.html http://www.cnblogs.com/smileberry/p/4300849.html