2012-05-01 28 views
12

は、私はそれが編集モードの場合、 Enterキーを次のセルへEnterキーで次のセルにフォーカスを移動WPF DataGridでを押しますか?

  1. 移動も押されてできるカスタムデータグリッドを持っていると思います。
  2. 現在の行の最後の列が到達すると、フォーカスは次の行の最初のセルに移動する必要があります。
  3. 次のセルに到達すると、そのセルが編集可能な場合、自動的に編集可能になります。
  4. セルにcomboboxcolumn以外のComboBoxが含まれている場合、コンボボックスはDropDownOpenである必要があります。

私を助けてください。私は、カスタムデータグリッドを作成することによって、過去数日からしようと

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クラスのワットを拡張すると – Mussammil

7

はるかに簡単な実装です。アイデアは、キーダウンイベントをキャプチャし、キーが "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)); 
    } 
} 
関連する問題