Search code examples
wpfcanvasfocusroutedcommand

WPF Routed Command only fires sometimes


I have some RoutedCommands for commands like control-A, copy paste and they all work fine. Then I added 4 more routedcommands to move object up down left and right in the canvas using arrowkeys, they sometimes works and sometime doesn't. At first I thought it was a Focus issue on the Canvas but I just found out that at the same time, all the other routedcommands like control-A works but arrowkeys doesn't. I really have no idea what's going on here, they are identical routedcommands with different variable names, how come one works 100% of time and one only work 50% of time?

Working RoutedCommand:

_bindings.Add(new CommandBinding(DesignerCanvas.SelectAll, SelectAll_Executed));
SelectAll.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));

private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
    SelectionService.SelectAll();
}

Malfunctioning RoutedCommand:

_bindings.Add(new CommandBinding(DesignerCanvas.MoveDown, MoveDown_Executed));
MoveDown.InputGestures.Add(new KeyGesture(Key.Down));

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
    var selectedItems = from item in SelectionService.CurrentSelection.OfType<DesignerItem>()
                            select item;

    if (selectedItems.Count() > 0)
    {
        for (int i = 0; i < selectedItems.Count(); i++)
            selectedItems.ElementAt(i).Top += Option.OptionSingleton.Sensitivity;
    }
}

The malfunctioning RoutedCommand is just not firing sometimes, especially after I open some other window and come back to the canvas, then it will stop firing while other routedcommands are unaffected. Any ideas what's causing this weird behavior?


Solution

  • It turns out that it was a focus issue, I just set the focus to the canvas whenever mouse enters, now it's sort of fixed. Thanks everybody for answering.