How to Close a Window in WPF on a escape key

前端 未结 4 2090
青春惊慌失措
青春惊慌失措 2020-12-24 10:49

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

4条回答
  •  时光取名叫无心
    2020-12-24 11:16

    Here is a button-less solution that is clean and more MVVM-ish. Add the following XAML into your dialog/window:

    
      
    
    
    
      
    
    

    and handle the event in the code-behind:

    private void CloseCommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
    {
      if (MessageBox.Show("Close?", "Close", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
        this.Close();
    }
    

提交回复
热议问题