[ACCEPTED]-How does the WPF Button.IsCancel property work?-button

Accepted answer
Score: 35

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

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

public MainWindow()
{
    InitializeComponent();

    this.PreviewKeyDown += new KeyEventHandler(CloseOnEscape);
}

private void CloseOnEscape(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Escape)
        Close();
}
Score: 17

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

yournamespace:WindowService.EscapeClosesWindow="True"

Here's the code for the 1 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();
   }
}
Score: 6

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

Score: 2

Yes this is right.In windows Application 9 in WPF AcceptButton and Cancel Button is 8 there. But one thing is that if you are 7 setting your control visibility as false, then 6 it won't work as expected.For that you need 5 to make as visibility as true in WPF. For 4 example: (it is not working for Cancel button 3 because here visibility is false)

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click" Visibility="Hidden"></Button> 

So, you 2 need make it:

<Button x:Name="btnClose" Content="Close" IsCancel="True" Click="btnClose_Click"></Button>

Then you have write btnClose_Click in code 1 behind file:

private void btnClose_Click (object sender, RoutedEventArgs e)
    { this.Close(); }

More Related questions