Create Key binding in WPF

后端 未结 5 1887
滥情空心
滥情空心 2020-11-30 08:47

I need to create input binding for Window.

public class MainWindow : Window
{
    public MainWindow()
    {
        SomeCommand = ??? () => OnAction();
           


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 09:25

    You will have to create your own Command implementing ICommand interface and initialize SomeCommand with the instance of that Command.

    Now you have to set the DataContext of Window to self in order to make the Command Binding work:

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

    OR you will have to update your Binding as

     
       
        
       
     
    

提交回复
热议问题