wpf mvvm加速键绑定

时间:2016-05-26 07:47:21

标签: wpf xaml mvvm menu mvvm-light

我是WPF和Mvvm的新手,尝试用加速键来理解菜单。我有以下xaml。帮助和子菜单H1,H2按预期工作,即按Alt + H然后按2,处理程序MenuItem_Click_2被调用。这很好,但我想用Mvvm做这个。如果你看到_View菜单(这里是Mvvm),只有Alt + V可以工作,这显示了与ViewModel绑定的菜单如下。但进一步说,如果我键入Alt + V然后键入G或P,它就不起作用。我想我错过了一些非常基本的东西。它是什么?

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>

1 个答案:

答案 0 :(得分:2)

Controls支持KeyBinding。例如,ItemsControl可以有各种KeyBinding'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}"/>