2012-04-23 20 views
5

Mam DataGrid w WPF.Używanie klawisza Enter jako tab w WPF DataGrid

chcę przejść do NextCell kiedy uderzę Wprowadź i po osiągnięciu LastColumn powinien mieć domyślną Wprowadź funkcję tworzenia lub przejście do następnego rzędu.

nie chcę używać Tab

Jak mogę to zrobić w WPF.

+0

Co próbowaliście? Wydaje się, że byłoby całkiem łatwo dołączyć do zdarzenia 'KeyUp' i obsłużyć' 'naciśnij ... – Tejs

+0

Ale nie działa –

+0

dgrow.MoveFocus (new TraversalRequest (FocusNavigationDirection.Next)); –

Odpowiedz

0

Wypróbuj to, myślę, że to zadziałało przynajmniej dla mnie.

//datagrid gotfocus event 
private void dataGrid1_GotFocus(object sender, RoutedEventArgs e) 
{ 
    DependencyObject dep = (DependencyObject)e.OriginalSource; 
    //here we just find the cell got focused ... 
    //then we can use the cell key down or key up 
    // iteratively traverse the visual tree 
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep == null) 
     return; 

    if (dep is DataGridCell) 
    { 
     DataGridCell cell = dep as DataGridCell; 
     //raise key down event of cell 
     cell.IsSelected = true; 
     cell.KeyDown += new KeyEventHandler(cell_KeyDown); 
    } 
} 

void cell_KeyDown(object sender, KeyEventArgs e) 
{ 
    DataGridCell cell = sender as DataGridCell; 
    if (e.Key == Key.Enter) 
    { 
     cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
     cell.IsSelected = false; 
     e.Handled = true; 
     cell.KeyDown -= cell_KeyDown; 
    } 
} 

w tym kodzie, gdy komórka ma fokus i klawisz użytkownika, na którym następna komórka zostanie ustawiona na ostrość. Powodzenia, mam nadzieję, że ci to pomoże.

EDIT:

ustawić tę funkcję jako DataGrid imprezy PreviewKeyDown.

private void maindg_PreviewKeyDown(object sender, KeyEventArgs e) 
     { 

      //just accept enter key 
      if (e.Key != Key.Enter) return; 

     DependencyObject dep = (DependencyObject)e.OriginalSource; 
     //here we just find the cell got focused ... 
     //then we can use the cell key down or key up 
     // iteratively traverse the visual tree 
     while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     if (dep == null) 
      return; 

     if (dep is DataGridCell) 
     { 
      //cancel if datagrid in edit mode 
      maindg.CancelEdit(); 
      //get current cell 
      DataGridCell cell = dep as DataGridCell; 
      //deselect current cell 
      cell.IsSelected = false; 
      //find next right cell 
      var nextCell = cell.PredictFocus(FocusNavigationDirection.Right); 
      //if next right cell null go for find next ro first cell 
      if (nextCell == null) 
      { 
       DependencyObject nextRowCell; 
       nextRowCell = cell.PredictFocus(FocusNavigationDirection.Down); 
       //if next row is null so we have no more row Return; 
       if (nextRowCell == null) return; 
       //we do this because we cant use FocusNavigationDirection.Next for function PredictFocus 
       //so we have to find it this way 
       while ((nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left) != null) 
        nextRowCell = (nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left); 
       //set new cell as next cell 
       nextCell = nextRowCell; 
      } 
      //change current cell 
      maindg.CurrentCell = new DataGridCellInfo(nextCell as DataGridCell); 
      //change selected cell 
      (nextCell as DataGridCell).IsSelected = true; 
      // start edit mode 
      maindg.BeginEdit(); 
     } 
     //handl the default action of keydown 
     e.Handled = true; 
    } 
+0

jego nie działa –

+0

@Mamad R, działa, mam wymaganie, gdy następna komórka zorientowała się, jak można ją edytować również – Mussammil

+0

@Mussammil Czytaj moją ostatnią edycję. Mam nadzieję, że ci pomogę :) –

-1

O wiele prostsza implementacja. Chodzi o to, aby uchwycić zdarzenie keydown i jeśli kluczem jest "Enter", przejdź do następnej zakładki, która jest następną komórką siatki.

/// <summary> 
/// On Enter Key, it tabs to into next cell. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    var uiElement = e.OriginalSource as UIElement; 
    if (e.Key == Key.Enter && uiElement != null) 
    { 
     e.Handled = true; 
     uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
    } 
}