2012-04-23 93 views
5

我在WPF中有DataGrid在WPF DataGrid中使用Enter鍵作爲選項卡

我想移動到NextCell時,我打輸入,並在達到LastColumn時,它應該有默認的輸入創建或移動到下一行的功能。

我不想使用標籤

我怎麼能在WPF做到這一點。

+0

你有什麼試過的?這聽起來像是很容易附加到'KeyUp'事件並處理一個''按... – Tejs 2012-04-23 20:32:09

+0

但它不工作 – 2012-04-23 20:32:48

+0

dgrow.MoveFocus(新的TraversalRequest(FocusNavigationDirection.Next)); – 2012-04-23 20:33:12

回答

0

試試這個我認爲它的工作至少爲我工作。

//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; 
    } 
} 

在這段代碼中,當一個單元格獲得焦點並且用戶鍵入下一個單元格時將獲得焦點。 祝你好運希望這可以幫助你。

編輯:

設置此功能的DataGrid 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

其不工作 – 2012-05-07 21:43:28

+0

@Mamad R,它的工作,我有一個需求,當下一個單元格獲得焦點時如何使它可編輯也 – Mussammil 2013-12-13 04:39:54

+0

@Mussammil閱讀我的最後編輯。希望能幫到你:) – 2013-12-15 07:50:12

-1

一個簡單得多的實現。這個想法是捕獲keydown事件,如果鍵是「Enter」,則移動到下一個選項卡,該選項卡是網格的下一個單元格。

/// <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)); 
    } 
}