J'ai déclaré <InputBindings>
C#/WPF: KeyBinding ne peut pas déclencher de commande
<UserControl.InputBindings>
<KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding CopyImageCommand}" />
<KeyBinding Key="V" Modifiers="Ctrl" Command="{Binding PasteImageCommand}" />
</UserControl.InputBindings>
Pour des fins de test, j'ai boutons ajouté liés à ces commandes trop
<Button Command="{Binding CopyImageCommand}" Content="Copy" />
<Button Command="{Binding PasteImageCommand}" Content="Paste" />
J'ai remarqué que lorsque le bouton de pâte est activé, quand j'appuie sur Ctrl-V rien ne se passe. Ctrl-C semble fonctionner. Pour cela, un élément de zone de liste est sélectionné, je ne suis pas sûr si cela fait une différence. Quelqu'un sait pourquoi mon PasteImageCommand
ne se déclenche pas?
J'utilise .NET 4 BTW
MISE À JOUR
Un code plus complet 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="*" />
MISE À JOUR
J'ai découvert que je dois mettre la KeyBindings
dans la MainWindow, mais les commandes sont dans le ViewModel
, comment puis-je définir des liaisons de touches dans le ShellView
qui se lie ensuite aux commandes dans le ShellViewModel
?
Pouvez-vous s'il vous plaît poster où sont spécifiés InputBinding? Il est possible que vous le mettiez au mauvais endroit. – Euphoric
@Euphoric, j'ai mis mes InputBindings dans le UserControl ShellView. J'ai découvert que cela fonctionnait quand je les ai mis dans MainWindow, mais j'ai besoin de définir le modèle de vue à ShellViewModel, pas vraiment correct je pense, comment puis-je gérer cela? –
@JiewMeng: Salut jiew! J'ai presque le même problème. Avez-vous trouvé une solution? – Jalal