2012-05-01 106 views
12

我想有能,也壓將焦點移至下一個單元格上按Enter鍵WPF DataGrid中按?

  1. 移動到下一個單元格時輸入關鍵,如果它是在編輯模式下自定義數據網格。
  2. 噹噹前行的最後一列到達時,焦點應移至下一行的第一個單元格。
  3. 在到達下一個單元格時,如果單元格可編輯,它應該自動變爲可編輯。
  4. 如果單元格包含不是組合框的ComboBox,則組合框應該爲DropDownOpen。

請幫我這個。我一直試圖從過去幾天創建一個自定義DataGrid並寫了一些代碼

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e) 

但我失敗了。

回答

5
private void dg_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    try 
    { 
     if (e.Key == Key.Enter) 
     { 
      e.Handled = true; 
      var cell = GetCell(dgIssuance, dgIssuance.Items.Count - 1, 2); 
      if (cell != null) 
      { 
       cell.IsSelected = true; 
       cell.Focus(); 
       dg.BeginEdit(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox(ex.Message, "Error", MessageType.Error); 
    } 
} 

public static DataGridCell GetCell(DataGrid dg, int row, int column) 
{ 
    var rowContainer = GetRow(dg, row); 

    if (rowContainer != null) 
    { 
     var presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 
     if (presenter != null) 
     { 
      // try to get the cell but it may possibly be virtualized 
      var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      if (cell == null) 
      { 
       // now try to bring into view and retreive the cell 
       dg.ScrollIntoView(rowContainer, dg.Columns[column]); 
       cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      } 
      return cell; 
     } 
    } 
    return null; 
} 
+0

我在幾個項目中做了類似的事情,代碼非常非常相似。我只是擴展了數據網格並封裝了大量你正在做的事情。所以我可以保證這個/你的答案絕對有效! +1 –

+15

這將是很高興看到缺少的代碼:'GetRow()','GetVisualChild ()'什麼是'dgIssuance'? – bitbonk

+0

爲什麼要將硬編碼值傳遞給GetCell函數?問題是當按下Enter鍵時移動到** next **單元格,但是您想要在Enter單擊的第2列中獲取最後一個單元格。爲什麼? – AndreyS

2
public class DataGrid : System.Windows.Controls.DataGrid 
{ 
    private void PressKey(Key key) 
    { 
     KeyEventArgs args = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key); 
     args.RoutedEvent = Keyboard.KeyDownEvent; 
     InputManager.Current.ProcessInput(args); 
    } 
    protected override void OnCurrentCellChanged(EventArgs e) 
    { 
     if (this.CurrentCell.Column != null)     
      if (this.CurrentCell.Column.DisplayIndex == 2) 
      { 

       if (this.CurrentCell.Item.ToString() == "--End Of List--") 
       { 
        this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down)); 
       } 
      } 
      else if (this.CurrentCell.Column != null && this.CurrentCell.Column.DisplayIndex == this.Columns.Count() - 1) 
      { 
       PressKey(Key.Return); 
       DataGridCell cell = DataGridHelper.GetCell(this.CurrentCell); 
       int index = DataGridHelper.GetRowIndex(cell); 
       DataGridRow dgrow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(this.Items[index]); 
       dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); 
      } 
    } 
    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 = UIHelper.GetVisualChild<DataGridCellsPresenter>(rowContainer); 
       if (columnIndex == 0) 
       { 
        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); 
       } 
       this.SelectedItem = this.CurrentItem; 
       e.Handled = true; 
       this.UpdateLayout(); 
      } 
     } 
    } 
} 

暫且,我寫了這個和它的工作對我來說。

+0

我收到錯誤'錯誤找不到'my:DataGridComboBoxColumn'類型。確認你沒有遺漏程序集引用,並且所有引用的程序集都已經編譯完成。'當我擴展datagrid類wats錯誤 – Mussammil

7

一個簡單得多的實現。這個想法是捕獲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)); 
    } 
} 
相關問題