我已经声明<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
主窗口中,但命令在 中ViewModel
,我如何在ShellView
其中设置键绑定然后绑定到中的命令ShellViewModel
?