现在的位置: 首页 > 综合 > 正文

WPF DataGrid 遇到的小问题

2012年04月03日 ⁄ 综合 ⁄ 共 2277字 ⁄ 字号 评论关闭

WPF DataGrid 遇到的小问题

        1) 在使用WPF的DataGrid时,虽然在绑定数据时使用的mode是twoway,

但在具体使用的时候,因为界面输入过快,数据的传输具有一定的延迟。
这样会有时导致异常的出现,解决这个办法我们可以自己调用函数datagrid.commiteEdit();
来手动传输数据。
        2)在输入数据后,界面没有及时的更新某些信息,如行数,以及在删除某行后,
行数没有重新排列。只要使用datagir.Items.Refresh();  就可以使得界面刷新。

       3)获得DataGrid中的某一行或者某一个单元格


        #region DataGrid Cell Row

        /// <summary>
        /// get the row of datagrid
        /// </summary>
        /// <param name="Index"></param>  [0,n-1]
        /// <returns>DataGridRow</returns> 
        private DataGridRow getRow(int Index)
        {
            try
            {
                DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index);
                if (row == null)
                {
                    dataGrid.UpdateLayout();
                    dataGrid.ScrollIntoView(dataGrid.Items[Index]);
                    row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index);
                }
                return row;
            }
            catch (Exception ex)
            {
                log.Error("getRow()----- exception occurred," + ex.Message);
                return null;
            }
        }
 
        /// <summary>
        /// get the cell of dataGrid by an existing row
        /// </summary>
        /// <param name="rowIndex"></param>  [0,n-1]
        /// <param name="columnIndex"></param>  [0,n-1]
        /// <returns>DataGridCell</returns>
        private DataGridCell getCell(int rowIndex, int columnIndex)
        {
            try
            {
                DataGridRow rowContainer = getRow(rowIndex);
               
                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = getVisualChild<DataGridCellsPresenter>(rowContainer);
                    
                    if (presenter == null)
                    {
                        dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
                        presenter = getVisualChild<DataGridCellsPresenter>(rowContainer);
                    }
                    
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    if (cell == null)
                    {
                        dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                    }

                    return cell;
                } // end of if (rowContainer != null)

            }
            catch (Exception ex)
            {
                log.Error("getCell()---- exception occurred, " + ex.Message);
            }
            return null;
        }

        /// <summary>
        /// get the visual child
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        private static T getVisualChild<T>(Visual parent) where T : Visual
        {
            try
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = getVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
            catch (Exception ex)
            {
                log.Error("getVisualChild()---- exception occurred, " + ex.Message);
                return null;
            }
        }

        #endregion

抱歉!评论已关闭.