J'ai un contrôle à l'intérieur d'un canevas et je veux pouvoir le déplacer en utilisant les touches fléchées. Par souci d'essayer les choses, j'ai créé la classe suivante, qui fait ce que je veux.Attacher/lier le même ensemble de commandes à plusieurs contrôles
<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CanvasControlArrowKeyTest" Height="300" Width="300">
<Canvas>
<Canvas.InputBindings>
<KeyBinding Key="Down" Command="MoveDown" />
<KeyBinding Key="Up" Command="MoveUp" />
<KeyBinding Key="Right" Command="MoveRight" />
<KeyBinding Key="Left" Command="MoveLeft" />
</Canvas.InputBindings>
<Button>
<Button.CommandBindings>
<CommandBinding Command="MoveDown" Executed="MoveDown_Executed" />
<CommandBinding Command="MoveUp" Executed="MoveUp_Executed" />
<CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" />
<CommandBinding Command="MoveRight" Executed="MoveRight_Executed" />
</Button.CommandBindings>
</Button>
</Canvas>
</Window>
Voici un extrait du code-behind:
private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
var uiElement = (UIElement)sender;
double value = Canvas.GetTop(uiElement);
value = Double.IsNaN(value) ? 0 : value;
value++;
Canvas.SetTop(uiElement, value < 0 ? 0 : value);
}
Tout cela fonctionne très bien, mais ce que je veux vraiment est un tas de boutons avec cette capacité, non seulement celui-là. Comment puis-je m'assurer que chaque bouton a ces CommandBindings? S'il y a un moyen plus simple que d'utiliser CommandBindings, qu'est-ce que cela pourrait être?
Mise à jour: Sur demande, voici une autre méthode qui ne semble pas fonctionner:
<Window x:Class="DiagramDesigner.CanvasControlArrowKeyTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CanvasControlArrowKeyTest" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="MoveDown" Executed="MoveDown_Executed" />
<CommandBinding Command="MoveUp" Executed="MoveUp_Executed" />
<CommandBinding Command="MoveLeft" Executed="MoveLeft_Executed" />
<CommandBinding Command="MoveRight" Executed="MoveRight_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="Down" Command="MoveDown" />
<KeyBinding Key="Up" Command="MoveUp" />
<KeyBinding Key="Right" Command="MoveRight" />
<KeyBinding Key="Left" Command="MoveLeft" />
</Window.InputBindings>
<Canvas >
<Button Width="50" Height="50" />
</Canvas>
</Window>
C#
private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
var uiElement = (UIElement)e.OriginalSource; // Still doesn't point to the Button
double value = Canvas.GetTop(uiElement);
value = Double.IsNaN(value) ? 0 : value;
value++;
Canvas.SetTop(uiElement, value < 0 ? 0 : value);
}
Mise à jour: J'ai renoncé à cette approche. J'ai fini par utiliser un different solution pour le problème, un qui n'utilise pas de commandes.
Cela semble prometteur, seulement il n'y a aucun événement pour les commandes que je utilise (c'est, Button a un événement Click, mais il n'y a pas Button.MoveUp, Button.MoveDown, etc.). Je vais devoir lire à ce sujet pour voir si je peux le faire fonctionner. – Pat
Je suppose qu'un événement routé est vraiment la solution que je suis allé avec: http://stackoverflow.com/questions/3992278/move-a-control-inside-a-canvas-using-directional-keys/3998438#3998438 – Pat