wpf 如何获取 InputBinding-Command 的发送者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14018794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:45:43  来源:igfitidea点击:

How to get the sender of the InputBinding-Command

wpfcommandkey-bindingsrelaycommand

提问by David

I have this xaml code:

我有这个 xaml 代码:

<Window.InputBindings>
    <KeyBinding Command="{Binding Path=KeyEnterCommand}" Key="Enter" />
</Window.InputBindings>

and that's the Code in my ViewModel:

这就是我的 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?

现在是我的问题,我怎样才能得到这个命令的发送者?

回答by Pavlo Glazkov

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
}

回答by Simon Stanford

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.

您还可以使用 CommandTarget 属性。这是一种稍微不同的用法,因为它使用 WPF 附带的预定义命令。但是,我不确定这是否/如何与从 ICommand 继承的类一起使用。

An article on wpf.2000things.comsays:

wpf.2000things.com上的一篇文章说:

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.

路由命令的源是调用命令的元素。Executed 或 CanExecute 处理程序中的 sender 参数是拥有事件处理程序的对象。将按钮的命令参数设置为特定命令,然后将该命令绑定到主窗口的 CommandBindings 中的某些代码,按钮是源,窗口是发送者。

设置 Command 属性时,您还可以设置 CommandTarget 属性,指示应将其视为路由命令源的不同元素。

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:

在下面的示例中,Find 命令现在似乎源自 TextBox。我们可以在 Executed 事件的事件处理程序中看到这一点:

<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>