非編集状態のキーボード操作(ショートカットキー)の変更方法

文書番号 : 34755     文書種別 : 使用方法     登録日 : 2012/12/19     最終更新日 : 2012/12/19
文書を印刷する
対象製品
SPREAD for WPF 1.0J
詳細
セル上のキーボード操作(ショートカットキー)を変更したい場合は.NET Frameworkで提供されているInputBindingsを使用して変更することができます。

【製品ヘルプ】
PowerTools SPREAD for WPF 1.0J
 - 開発者ガイド
  - キーボード操作

この方法でキーボード操作を変更した場合、編集中セルと非編集中セルの両方で設定したコマンドが有効になります。編集中セルと非編集中セルにおいて、各々別のコマンドを設定したい場合は下記のようにICommandインタフェースを実装した独自のコマンドを実装する必要があります。

このサンプルではEnterキーの動作を下記のように変更しています。
 セル編集中:セルの編集を完了する
 セル非編集中:アクティブセルを次行へ移動する

◎サンプルコード(VB)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
  GcSpreadGrid1.InputBindings.Add(New InputBinding(New MyCommand(Me.GcSpreadGrid1), New KeyGesture(Key.Enter)))
End Sub

Public Class MyCommand
  Implements ICommand
  Private _gcSpreadGrid As GrapeCity.Windows.SpreadGrid.GcSpreadGrid
  Public Sub New(gcSpreadGrid As GrapeCity.Windows.SpreadGrid.GcSpreadGrid)
    _gcSpreadGrid = gcSpreadGrid
  End Sub
  Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
    Return True
  End Function

  Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

  Public Sub Execute(parameter As Object) Implements ICommand.Execute
    If _gcSpreadGrid.EditElement IsNot Nothing Then
      _gcSpreadGrid.EditCommands.CommitCellEdit.Execute(Nothing)
    Else
      _gcSpreadGrid.NavigationCommands.MoveDown.Execute(Nothing)
    End If
  End Sub
End Class


◎サンプルコード(C#)
private void Window_Loaded(object sender, RoutedEventArgs e){
  gcSpreadGrid1.InputBindings.Add(new InputBinding(new MyCommand(GcSpreadGrid1), new KeyGesture(Key.Enter)));
}

public class MyCommand : ICommand
{
  private GrapeCity.Windows.SpreadGrid.GcSpreadGrid _gcSpreadGrid;
  public MyCommand(GrapeCity.Windows.SpreadGrid.GcSpreadGrid gcSpreadGrid)
  {
    _gcSpreadGrid = gcSpreadGrid;
  }
  public bool CanExecute(object parameter)
  {
    return true;
  }
  public event EventHandler CanExecuteChanged;

  public void Execute(object parameter)
  {
    if (_gcSpreadGrid.EditElement != null) {
      _gcSpreadGrid.EditCommands.CommitCellEdit.Execute(null);
    } else {
      _gcSpreadGrid.NavigationCommands.MoveDown.Execute(null);
    }
  }
}

キーワード
入力 ショートカット