我已经声明了<InputBindings>
<UserControl.InputBindings>
<KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding CopyImageCommand}" />
<KeyBinding Key="V" Modifiers="Ctrl" Command="{Binding PasteImageCommand}" />
</UserControl.InputBindings>
为了测试目的,我添加了绑定到这些命令的按钮
<Button Command="{Binding CopyImageCommand}" Content="Copy" />
<Button Command="{Binding PasteImageCommand}" Content="Paste" />
我注意到当启用粘贴按钮时,按Ctrl-V键没有任何反应。Ctrl-C似乎可以工作。此时列表框中的项目被选中,我不确定这是否有任何区别。有人知道为什么我的PasteImageCommand
没有触发吗?我顺便提一下,我正在使用.NET 4。
更新:
以下是更完整的代码片段:
<UserControl x:Class="QuickImageUpload.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:QuickImageUpload.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.InputBindings>
<KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding CopyImageCommand}" />
<KeyBinding Key="V" Modifiers="Ctrl" Command="{Binding PasteImageCommand}" />
</UserControl.InputBindings>
<UserControl.DataContext>
<vm:ShellViewModel />
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
更新
我发现我需要将 KeyBindings
放在主窗口 (MainWindow
) 中,但是命令在视图模型 (ViewModel
) 中,如何在 ShellView
中设置键绑定,然后将其绑定到 ShellViewModel
的命令中?