How does the WPF Button.IsCancel property work? How does the WPF Button.IsCancel property work? wpf wpf

How does the WPF Button.IsCancel property work?


Yes, it only works on dialogs as a normal window has no concept of "cancelling", it's the same as DialogResult.Cancel returning from ShowDialog in WinForms.

If you wanted to close a Window with escape you could add a handler to PreviewKeyDown on the window, pickup on whether it is Key.Escape and close the form:

public MainWindow(){    InitializeComponent();    this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);}private void CloseOnEscape(object sender, KeyEventArgs e){    if (e.Key == Key.Escape)        Close();}


We can take Steve's answer one step further and create an attached property that provides the "escape on close" functionality for any window. Write the property once and use it in any window. Just add the following to the window XAML:

yournamespace:WindowService.EscapeClosesWindow="True"

Here's the code for the property:

using System.Windows;using System.Windows.Input;/// <summary>/// Attached behavior that keeps the window on the screen/// </summary>public static class WindowService{   /// <summary>   /// KeepOnScreen Attached Dependency Property   /// </summary>   public static readonly DependencyProperty EscapeClosesWindowProperty = DependencyProperty.RegisterAttached(      "EscapeClosesWindow",      typeof(bool),      typeof(WindowService),      new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnEscapeClosesWindowChanged)));   /// <summary>   /// Gets the EscapeClosesWindow property.  This dependency property    /// indicates whether or not the escape key closes the window.   /// </summary>   /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>   /// <returns>The value of the EscapeClosesWindow property</returns>   public static bool GetEscapeClosesWindow(DependencyObject d)   {      return (bool)d.GetValue(EscapeClosesWindowProperty);   }   /// <summary>   /// Sets the EscapeClosesWindow property.  This dependency property    /// indicates whether or not the escape key closes the window.   /// </summary>   /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>   /// <param name="value">value of the property</param>   public static void SetEscapeClosesWindow(DependencyObject d, bool value)   {      d.SetValue(EscapeClosesWindowProperty, value);   }   /// <summary>   /// Handles changes to the EscapeClosesWindow property.   /// </summary>   /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>   /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>   private static void OnEscapeClosesWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)   {      Window target = (Window)d;      if (target != null)      {         target.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(Window_PreviewKeyDown);      }   }   /// <summary>   /// Handle the PreviewKeyDown event on the window   /// </summary>   /// <param name="sender">The source of the event.</param>   /// <param name="e">A <see cref="KeyEventArgs"/> that contains the event data.</param>   private static void Window_PreviewKeyDown(object sender, KeyEventArgs e)   {      Window target = (Window)sender;      // If this is the escape key, close the window      if (e.Key == Key.Escape)         target.Close();   }}


This isn't quite right is it...MSDN says this: When you set the IsCancel property of a button to true, you create a Button that is registered with the AccessKeyManager. The button is then activated when a user presses the ESC key.So you do need a handler in your code behindAnd you don't need any attached properties or anything like that