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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:38:37  来源:igfitidea点击:

Create Key binding in WPF

c#wpfxaml

提问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 SomeCommandwith some CustomCommand : ICommandit doesn't fire. SomeCommandproperty 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 Commandimplementing ICommandinterface and initialize SomeCommandwith the instance of that Command.

您必须创建自己的Command实现ICommand接口并SomeCommand使用该实例进行初始化Command

Now you have to set the DataContextof Window to self in order to make the CommandBindingwork:

现在您必须将DataContextWindow设置为 self 才能完成CommandBinding工作:

public MainWindow()
{
    InitializeComponents();
    DataContext = this;
    SomeCommand = MyCommand() => OnAction();
}

OR you will have to update your Bindingas

或者你将不得不更新你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" >