2016-08-03 10 views
1

DataGridの既定の動作は、Enterキーを押したときに下に移動することです。私はRightに移動するように変更しようとしています。WPF DataGridでEnterキーを押したときに次の列に移動

私はthisを見て、以下のように実装しようとしました。それはちょっと働いて、Enterキーを押すと1列右に移動しますが、1列下に移動しています!

たとえば、私が55を含むセルにいるときにenterを押すと、20を含むセルになります。

私はそれをデバッグしましたが、問題の原因を見つけることができません。 セルの値はcell.Focus();で正しいですし、これが原因で何が起こったのかわかりません。奇妙な部分は、私が最後の行にいてEnterを押すと機能します。

------------------ 
| Depth | Width | 
------------------ 
| 55 | 30 | 
------------------ 
| 45 | 20 | 
------------------ 

private void PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    var grid = (DataGrid)sender; 

    if (e.Key == Key.Enter || e.Key == Key.Return) 
    { 
     var columnIndex = grid.Columns.IndexOf(MyDataGrid.CurrentColumn); 

     var rowIndex = grid.Items.IndexOf(MyDataGrid.CurrentItem); 

     // index of the next column 
     columnIndex = columnIndex + 1; 

     // reset column index if we are at the end of the row 
     if (columnIndex > grid.Columns.Count - 1) 
     { 
      rowIndex = rowIndex + 1; 
      columnIndex = 0; 

      // return if we have reached the last row 
      if (rowIndex > grid.Items.Count - 1) 
      { 
       return; 
      } 
     } 

     // there should always be a selected cell 
     if (grid.SelectedCells.Count != 0) 
     { 
      SelectCellByIndex(grid, rowIndex, columnIndex); 
     } 
    } 
} 

public static void SelectCellByIndex(DataGrid dataGrid, int rowIndex, int columnIndex) 
{ 
    if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.CellOrRowHeader)) 
     throw new ArgumentException("The SelectionUnit of the DataGrid must be set to Cell."); 

    if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1)) 
     throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex)); 

    if (columnIndex < 0 || columnIndex > (dataGrid.Columns.Count - 1)) 
     throw new ArgumentException(string.Format("{0} is an invalid column index.", columnIndex)); 

    dataGrid.SelectedCells.Clear(); 

    object item = dataGrid.Items[rowIndex]; //=Product X 
    DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
    if (row == null) 
    { 
     dataGrid.ScrollIntoView(item); 
     row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
    } 
    if (row != null) 
    { 
     DataGridCell cell = GetCell(dataGrid, row, columnIndex); 
     if (cell != null) 
     { 
      DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(cell); 
      dataGrid.SelectedCells.Add(dataGridCellInfo); 
      cell.Focus(); 
     } 
    } 
} 

public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column) 
{ 
    if (rowContainer != null) 
    { 
     DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer); 
     if (presenter == null) 
     { 
      /* if the row has been virtualized away, call its ApplyTemplate() method 
      * to build its visual tree in order for the DataGridCellsPresenter 
      * and the DataGridCells to be created */ 
      rowContainer.ApplyTemplate(); 
      presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer); 
     } 
     if (presenter != null) 
     { 
      DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; 
      if (cell == null) 
      { 
       /* bring the column into view 
       * in case it has been virtualized away */ 
       dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); 
       cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; 
      } 
      return cell; 
     } 
    } 
    return null; 
} 

public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject 
{ 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
     if (child != null && child is T) 
      return (T)child; 
     else 
     { 
      T childOfChild = FindVisualChild<T>(child); 
      if (childOfChild != null) 
       return childOfChild; 
     } 
    } 
    return null; 
} 

答えて

1

あなたは、EVを完了したらその理由は、おそらくですentは通常のプロセスを継続し、どこかで実行されます。あなたが何をしたいか

は、あなたがそれを処理してきた後、あなたのメソッドの最後にある、うわー真

// do your stuff 
e.Handled = true; 
// return here, and it should not go down as well 
+0

handledを設定!私はそれが何度か実行されていることと関係があると思っていましたが、それをどう対処するのか分かりませんでした!ありがとうございました! – Vahid

+1

問題はありません。それは喜んで:) – Noctis

関連する問題