WPF DataGrid 遇到的小問題

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章