How can I assign the 'Close on Escape-key press' behavior to all WPF windows within a project?

后端 未结 7 2181
长发绾君心
长发绾君心 2020-12-25 13:21

Is there any straightforward way of telling the whole WPF application to react to Escape key presses by attempting to close the currently focused widow? It is not a great bo

7条回答
  •  自闭症患者
    2020-12-25 13:41

    You can also use PreviewKeyDown Event

    PreviewKeyDown="UserControl_PreviewKeyDown"
    

    Code behind call you close command

    private void UserControl_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == Key.Escape)
                {
                    _vm.OnCloseCommand(sender);
                }
            }
    

提交回复
热议问题