gpt4 book ai didi

wpf mvvm 加速键绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:16:07 24 4
gpt4 key购买 nike

我是 WPF 和 Mvvm 的新手,试图用加速键理解菜单。我有以下 xaml。帮助和子菜单 H1、H2 按预期工作,即我按 Alt + H,然后按 2,将调用处理程序 MenuItem_Click_2。这很好用,但我想用 Mvvm 来做这件事。如果您看到 _View 菜单(这里是 Mvvm),则只有 Alt + V 有效,并且根据与 ViewModel 的绑定(bind)显示菜单如下。但除此之外,如果我输入 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>

最佳答案

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}"/>

关于wpf mvvm 加速键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37454643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com