2016-05-26 1 views
2

私はWPFとMvvmで初心者です。アクセラレータキーのメニューを理解しようとしています。私は次のxamlを持っています。ヘルプとサブメニューH1、H2は期待通りに機能します。つまり、Alt + Hを押してから2を押すと、MenuItem_Click_2というハンドラが呼び出されます。これは正常に動作しますが、私はMvvmでこれを行いたいと思います。 _Viewメニュー(ここにMvvmがあります)が表示されている場合、Alt + Vのみが機能し、ViewModelとのバインディングに従って、次のようなメニューが表示されます。しかし、さらにこれには、Alt + VとGまたはPを入力すると機能しません。私はここで非常に基本的な何かを逃していると思う。それは何ですか?wpf mvvmアクセラレータキーバインド

enter image description here

また_Helpは

enter image description here

<Window x:Class="Aitoe.Vigilant.Controller.WpfController.MultiCameraControllerView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:Aitoe.Vigilant.Controller.WpfController" 
     xmlns:localM="clr-namespace:Aitoe.Vigilant.Controller.WpfController.Model" 
     xmlns:localInfra="clr-namespace:Aitoe.Vigilant.Controller.WpfController.Infra" 
     xmlns:localCustomControls="clr-namespace:Aitoe.Vigilant.Controller.WpfController.CustomControls" 
     xmlns:localV="clr-namespace:Aitoe.Vigilant.Controller.WpfController.Views" 
     xmlns:localVM="clr-namespace:Aitoe.Vigilant.Controller.WpfController.ViewModel" 
     DataContext="{Binding MultiCameraControllerVM, Source={StaticResource Locator}}" 
     mc:Ignorable="d" WindowState="Maximized" 
     Title="Aitoe Multi Camera Controller" Height="300" Width="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <Menu> 
      <MenuItem Header="_File"></MenuItem> 
      <MenuItem Header="_View"> 
       <ItemsControl ItemsSource="{Binding PageViewModels}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <MenuItem Header="{Binding Name}" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
             CommandParameter="{Binding }" 
             /> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </MenuItem> 
      <MenuItem Header="_Help"> 
       <MenuItem Header="H_1" Click="MenuItem_Click_1" /> 
       <MenuItem Header="H_2" Click="MenuItem_Click_2"/> 
      </MenuItem> 
     </Menu> 
     <ContentControl Grid.Row="1" Content="{Binding CurrentPageViewModel}" /> 
    </Grid> 
</Window> 

答えて

2

ControlsKeyBindingのサポートを持って次のようになり、完全に動作します。例えば、ItemsControlKeyBinding's様々持つことができます。

<ItemsControl ItemsSource="{Binding FooData}"> 
     <ItemsControl.InputBindings> 
      <KeyBinding Modifiers="Alt" Key="V" Command="{Binding YourCommand}"/>    
     </ItemsControl.InputBindings>    
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
</ItemsControl> 

をまた、あなたは、複数の修飾子を設定することができます。

<KeyBinding Modifiers="Alt+Shift" Key="V" Command="{Binding YourCommand}"/> 
+0

私はそれを試してみると、あなたが知っているだろう。シンプルに見えます。ありがとう。 – VivekDev

+0

@VivekDevはどんな質問をすることも自由です。私の返事があなたに役立つと感じたら、私の返事を答えとしてマークして、将来の他の人の検索を簡単にすることができます。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workをご覧ください。 – StepUp