C# 在 WPF 中创建键绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697106/
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
Create Key binding in WPF
提问by deeptowncitizen
I need to create input binding for Window.
我需要为 Window 创建输入绑定。
public class MainWindow : Window
{
public MainWindow()
{
SomeCommand = ??? () => OnAction();
}
public ICommand SomeCommand { get; private set; }
public void OnAction()
{
SomeControl.DoSomething();
}
}
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding SomeCommand}" Key="F5"></KeyBinding>
</Window.InputBindings>
</Window>
If I init SomeCommand
with some CustomCommand : ICommand
it doesn't fire. SomeCommand
property getter is never called.
如果我SomeCommand
用一些初始化CustomCommand : ICommand
它不会触发。SomeCommand
永远不会调用属性 getter。
采纳答案by Aleksey
For your case best way used MVVM pattern
对于您的情况,使用 MVVM 模式的最佳方式
XAML:
XAML:
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding SomeCommand}" Key="F5"/>
</Window.InputBindings>
</Window>
Code behind:
后面的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
In your view-model:
在您的视图模型中:
public class MyViewModel
{
private ICommand someCommand;
public ICommand SomeCommand
{
get
{
return someCommand
?? (someCommand = new ActionCommand(() =>
{
MessageBox.Show("SomeCommand");
}));
}
}
}
Then you'll need an implementation of ICommand
.
This simple helpful class.
然后你需要一个ICommand
. 这个简单有用的课程。
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
回答by Nitin
You will have to create your own Command
implementing ICommand
interface and initialize SomeCommand
with the instance of that Command
.
您必须创建自己的Command
实现ICommand
接口并SomeCommand
使用该实例进行初始化Command
。
Now you have to set the DataContext
of Window to self in order to make the Command
Binding
work:
现在您必须将DataContext
Window设置为 self 才能完成Command
Binding
工作:
public MainWindow()
{
InitializeComponents();
DataContext = this;
SomeCommand = MyCommand() => OnAction();
}
OR you will have to update your Binding
as
或者你将不得不更新你Binding
的
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding SomeCommand, RelativeSource={RelativeSource Self}}" Key="F5"></KeyBinding>
</Window.InputBindings>
</Window>
回答by Paul-Sebastian Manole
For modifiers (key combinations):
对于修饰符(组合键):
<KeyBinding Command="{Binding SaveCommand}" Modifiers="Control" Key="S"/>
回答by Jitendra Pancholi
It might be too late but here is the simplest and shortest solution.
可能为时已晚,但这是最简单和最短的解决方案。
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
{
// Call your method here
}
}
<Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" >