Dav*_*vid 1 wpf command key-bindings relaycommand
我有这个xaml代码:
<Window.InputBindings>
<KeyBinding Command="{Binding Path=KeyEnterCommand}" Key="Enter" />
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)
这就是我的ViewModel中的代码:
private RelayCommand _keyEnterCommand;
public ICommand KeyEnterCommand
{
get
{
if (_keyEnterCommand == null)
{
_keyEnterCommand = new RelayCommand(param => ExecuteKeyEnterCommand());
}
return _keyEnterCommand;
}
}
public void ExecuteKeyEnterCommand()
{
// Do magic
}
Run Code Online (Sandbox Code Playgroud)
现在是我的问题,我怎样才能得到这个命令的发件人?
如果"发送者"是指按下键时聚焦的元素,则可以将当前聚焦的元素作为参数传递给命令,如下所示:
<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>
...
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9409 次 |
| 最近记录: |