protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter) { DataGridRow rowContainer = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(this.CurrentItem); if (rowContainer != null) { int columnIndex = this.Columns.IndexOf(this.CurrentColumn); DataGridCellsPresenter presenter = Helper.FindVisualChild <DataGridCellsPresenter>(rowContainer); if (columnIndex == Columns.Count() - 1) { DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); request.Wrapped = true; cell.MoveFocus(request); //BeginEdit(); PressKey(Key.Down); } else { CommitEdit(); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); request.Wrapped = true; cell.MoveFocus(request); } SelectedItem = CurrentItem; e.Handled = true; //UpdateLayout(); } } }
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e) { DataGrid dataGrid = sender as DataGrid; if (dataGrid == null) { return; } if (e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left) { if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == dataGrid.Items.Count - 2) { var border = VisualTreeHelper.GetChild(dataGrid, 0) as Decorator; if (border != null) { var scroll = border.Child as ScrollViewer; if (scroll != null) { scroll.ScrollToEnd(); } } } } if (e.Key == Key.Enter) { TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); request.Wrapped = true; DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem); if (rowContainer != null) { int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn); System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); dataGrid.CommitEdit(); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.MoveFocus(request); dataGrid.SelectedItem = dataGrid.CurrentItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } else if (e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left) { int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn); DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem); if (rowContainer != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell currentcell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); if (currentcell.Content is ContentPresenter) { DatePickerCus datepicker = FindVisualChild <DatePickerCus>(currentcell); if (datepicker != null && datepicker.IsDropDownOpen) { e.Handled = true; return; } ComboBoxWithSearch combo = FindVisualChild <ComboBoxWithSearch>(currentcell); if (combo != null && combo.IsDropDownOpen) { e.Handled = true; return; } } if (e.Key == Key.Down) { dataGrid.CommitEdit(); dataGrid.SelectedIndex++; rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem); if (rowContainer != null) { presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.Focus(); dataGrid.CurrentItem = dataGrid.SelectedItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } else if (e.Key == Key.Up) { if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == 0) { return; } dataGrid.CommitEdit(); dataGrid.SelectedIndex--; rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem); if (rowContainer != null) { presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.Focus(); dataGrid.CurrentItem = dataGrid.SelectedItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } } } }