1

私はこれを持っていますMainWindow.xaml

<Window.InputBindings>
  <KeyBinding Key="O" Modifiers="Control" Command="{Binding OpenCommand}" />
  <KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}" />
</Window.InputBindings>

独自のビューモデルを持つ子ビューがいくつかあります。たとえば、aFileViewとaFileViewModelとaDataViewとがありDataViewModelます。OpenCommand両方のビューモデルで、 :の実装があります。

public ICommand OpenCommand
{
    get
    {
        if (openCommand == null)
        {
            openCommand = new RelayCommand(param => this.OpenFile());
        }

        return openCommand;
    }
}

Ctrl+Oを押すOpenCommandと、アクティブなビューのビューモデルに対してコマンドが実行されます。したがって、のキーを押すと、FileViewOpenFile()実行されます。にキーを入力するとDataViewOpenData()が実行されます。ある種のMDI振る舞い。

上記のコードは機能しません。

このタイプのキーバインド/コマンド処理をどのように実装しますか?

4

1 に答える 1

1

OpenCommandinFileViewとの個別の実装があるため、これらのビューにもをDataView追加する必要があります。KeyBinding

<Page.InputBindings>
    <KeyBinding Key="O" Modifiers="Control" Command="{Binding OpenCommand}" />
    <KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}" />
</Page.InputBindings>

また

<UserControl.InputBindings>
    <KeyBinding Key="O" Modifiers="Control" Command="{Binding OpenCommand}" />
    <KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}" />
</UserControl.InputBindings>
于 2013-01-30T13:13:24.317 に答える