Pregunta

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?

¿Fue útil?

Solución

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:

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="root">
    <Window.InputBindings>
        <KeyBinding Command="{Binding Path=KeyEnterCommand}"
                    CommandParameter="{Binding ElementName=root, Path=(FocusManager.FocusedElement)}"
                    Key="Escape" />
    </Window.InputBindings>
...

private RelayCommand<object> _keyEnterCommand;

public ICommand KeyEnterCommand
{
    get
    {
        if (_keyEnterCommand == null)
        {
            _keyEnterCommand = new RelayCommand<object>(ExecuteKeyEnterCommand);
        }

        return _keyEnterCommand;
    }
}

public void ExecuteKeyEnterCommand(object sender)
{
    // Do magic
}

Otros consejos

You can also use the CommandTarget property. This is a slightly different usage as it uses the predefined commands that come with WPF. However, I'm not sure if/how this can be used with classes that inherit from ICommand.

An article on wpf.2000things.com says:

The source of a routed command is the element that is invoking the command. The sender parameter in the Executed or CanExecute handlers is the object that owns the event handler. Setting the Command parameter of a Button to a particular command and then binding the command to some code in the CommandBindings for a main Window, the button is the source and the window is the sender.

When setting the Command property, you can also set the CommandTarget property, indicating a different element that should be treated as the source of the routed command.

In the example below, the Find command now appears to originate from the TextBox. We can see this in the event handler for the Executed event:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Commands" Width="320" Height="220">

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Find"
                        CanExecute="Find_CanExecute"
                        Executed="Find_Executed"/>
    </Window.CommandBindings>

    <StackPanel>
        <TextBox Name="txtSomeText"
                 Width="140" Height="25" Margin="10"/>
        <Button Content="Find"
                Command="ApplicationCommands.Find"
                CommandTarget="{Binding ElementName=txtSomeText}"
                Margin="10" Padding="10,3"
                HorizontalAlignment="Center" />
    </StackPanel>
</Window>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top