I have this xaml code:
<Window.InputBindings>
<KeyBinding Command="{Binding Path=KeyEnterCommand}" Key="Enter" />
</Window.InputBindings>
and that’s the Code in my ViewModel:
private RelayCommand _keyEnterCommand;
public ICommand KeyEnterCommand
{
get
{
if (_keyEnterCommand == null)
{
_keyEnterCommand = new RelayCommand(param => ExecuteKeyEnterCommand());
}
return _keyEnterCommand;
}
}
public void ExecuteKeyEnterCommand()
{
// Do magic
}
Now is my question, how can i get the sender of this command?
If by “sender” you mean the element that was focused when the key was pressed, then you can pass the currently focused element as a parameter to the command, like this: