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

后端 未结 7 2180
长发绾君心
长发绾君心 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 14:00

    None of above worked for me, except Kai's. I modified his answer: I added 'btn_close.IsCancel = true;' to constructor. SettingsWindow is my second window, and main window is (default) MainWindow.

      public partial class SettingsWindow : Window {
        public SettingsWindow() {
          InitializeComponent();
          btn_close.IsCancel = true;
        }
        private void btn_close_Click(object sender, RoutedEventArgs e) {
          this.Close();
        }
      }
    

    Hope it helps,

    Simon S love nia

    0 讨论(0)
提交回复
热议问题