【WPF】DataGrid编辑单元格【回车触发事件】

DataGrid编辑单元格回车将焦点移动到下一个单元格

使用DataGrid的PreviewKeyDown方法

 /// 
        /// 回车将焦点移动到下一个单元格
        /// 
        /// 
        /// 
        private void RepairItems_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    e.Handled = true;
                    Int32 row = this.repairItems.Items.IndexOf(this.repairItems.CurrentItem);
                    Int32 Col = this.repairItems.Columns.IndexOf(this.repairItems.CurrentColumn);
                    var cell = GetCell(this.repairItems, row, Col + 1);
                    if (cell != null)
                    {
                        cell.IsSelected = true;
                        cell.Focus();
                        this.repairItems.BeginEdit();
                    }
                }
            }
            catch (Exception )
            {
                throw;
            }
        }
 
        /// 
        /// 根据行、列索引取的对应单元格对象
        /// 
        /// 
        /// 
        /// 
        public DataGridCell GetCell(DataGrid grid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(grid, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                if (presenter == null)
                {
                    grid.ScrollIntoView(rowContainer, grid.Columns[column]);
                    presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                }
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                return cell;
            }
            return null;
        }
 
        /// 
        /// 根据行索引取的行对象
        /// 
        /// 
        /// 
        public DataGridRow GetRow(DataGrid grid, int index)
        {
            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            { 
                grid.ScrollIntoView(grid.Items[index]);
                row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }
 
        /// 
        /// 获取指定类型的子元素
        /// 
        /// 
        /// 
        /// 
        private T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual visual = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = visual as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(visual);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

你可能感兴趣的:(WPF-grid)