我宣布<InputBindings>
C#/ WPF:键绑定不触发命令
<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顺便说一句
UPDATE
更全面的代码snipplet
<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="*" />
UPDATE
我发现我需要把KeyBindings
在MainWindow中,但是命令在ViewModel
中,我如何在ShellView
中设置键绑定,然后绑定到ShellViewModel
中的命令?
能否请您发布的InputBinding指定在哪里?可能你把它放在错误的地方。 – Euphoric 2010-10-15 11:00:39
@Euphoric,我把我的InputBindings放在UserControl ShellView中。当我将它们放在MainWindow中时,我发现它可以工作,但我需要将视图模型设置为ShellViewModel,我认为这不是很正确,我该如何处理? – 2010-10-19 02:44:23
@JiewMeng:你好!我有几乎相同的问题。你有没有找到解决办法? – Jalal 2012-03-26 21:22:54