• 获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell


    C# code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
            /// <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;
                }
            }
  • 相关阅读:
    [usaco3.2.5]msquare
    [usaco3.2.4]ratios
    [usaco3.2.3]spin
    [文献记录] Few-shot Learning for Named Entity Recognition in Medical Text 医学文本中命名实体识别的小样本学习
    计算机保研经验分享
    文本处理、词频统计与Simhash生成文档指纹
    [知乎live笔记]如何得到好的科研Idea
    POJ 2787:算24
    POJ 2964:日历问题 日期转换+闰年月份可放在一个month[2][12]数组里
    POJ-1835 宇航员 空间方向模拟+打表
  • 原文地址:https://www.cnblogs.com/sjqq/p/8032201.html
Copyright © 2020-2023  润新知