I have some inputbindings in a Window:
<Window.InputBindings>
<KeyBinding Key="Space" Command="{Binding Path=StepNext}" />
<KeyBinding Key="R" Command="{Binding Path=ResetSong}" />
<KeyBinding Key="Left" Command="{Binding Path=StepPrevious}" />
</Window.InputBindings>
The commands are defined in my viewmodel as RelayCommands:
public class RelayCommand : ICommand
{
private Action<object> _exec;
private Func<object, bool> _canExec;
public RelayCommand(Action<object> exec, Func<object, bool> canExec)
{
_exec = exec;
_canExec = canExec;
}
public void Execute(object parameter)
{
_exec(parameter);
}
public bool CanExecute(object parameter)
{
return _canExec(parameter);
}
public event EventHandler CanExecuteChanged;
}
In the ViewModel's constructor:
StepNext = new RelayCommand(DoStepNext, CanStepNext);
This works fine. However, whenever I select an item in a listbox (which is a child of the Window), the KeyBindings stop working. How can I make the parent capture the keybindings regardless of which child has focus (if any). (shouldn't the events bubble up anyway?)
I'm aware that there is a PreviewKeyDown event that does just this, but that would make my ICommand
s useless, so I'd prefer a declarative solution if possible.
Thanks in advance.