zoukankan      html  css  js  c++  java
  • 获取WPF的DataGrid控件中,是否存在没有通过错误验证的Cell

     /// <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;
                }
            }
  • 相关阅读:
    leetcode Move Zeroes
    leetcode Same Tree
    leetcode range sum query
    leetcode Invert Binary Tree
    leetcode【sql】 Delete Duplicate Emails
    mac编译PHP报错 configure: error: Please reinstall the libcurl distribution
    Linux添加系统环境变量的两种方法
    Mysql获取去重后的总数
    MySQL查询order by相减select相减的Sql语句
    修改maven本地仓库路径
  • 原文地址:https://www.cnblogs.com/sjqq/p/8486926.html
Copyright © 2011-2022 走看看