2016-08-03 2 views
1

DataGrid의 기본 동작은 Enter 키를 누르면 아래로 이동합니다. 나는 그것을 바꾸려고 노력하고있다. Right.WPF DataGrid에서 Enter 키를 누를 때 다음 열로 이동

나는 this을 보았으며 아래와 같이 구현하려고 시도했다. 그것은 다소 일하고 있고 Enter 키를 누를 때 오른쪽으로 한 칸 이동하지만 한 줄 아래로 움직이고 있습니다!

예를 들어 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 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


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; 
} 
이유는 아마도이 완료되면 있기 때문에,이 EV ent는 정상적인 프로세스를 계속 진행하고 어딘가에는 완료됩니다. 당신이 그것을 처리 한 후

싶은 것은 당신의 방법의 끝에,의 handled

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

사실
+0

와우로 설정! 나는 그것이 여러 번 실행하는 것과 관련이 있다고 의심하지만 어떻게 처리해야할지 몰랐다! 여러 번 고마워! – Vahid

+1

문제가없는 친구입니다. 다행히 도와 줬어 :) – Noctis