Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the WPF Button.IsCancel property work?

Tags:

button

wpf

The basic idea behind a Cancel button is to enable closing your window with an Escape Keypress.

You can set the IsCancel property on the Cancel button to true, causing the Cancel button to automatically close the dialog without handling the Click event.

Source: Programming WPF (Griffith, Sells)

So this should work

<Window> <Button Name="btnCancel" IsCancel="True">_Close</Button> </Window> 

However the behavior I expect isn't working out for me. The parent window is the main application window specified by the Application.StartupUri property. What works is

<Button Name="btnCancel" IsCancel=True" Click="CloseWindow">_Close</Button>  private void CloseWindow(object sender, RoutedEventArgs)  {     this.Close(); } 
  • Is the behavior of IsCancel different based on whether the Window is a normal window or a Dialog? Does IsCancel work as advertised only if ShowDialog has been called?
  • Is an explicit Click handler required for the button (with IsCancel set to true) to close a window on an Escape press?
like image 341
Gishu Avatar asked Jan 07 '09 08:01

Gishu


People also ask

What is a WPF button?

The Button control is one of the basic controls in WPF. A button is used to click and execute code on its click event handler. A button control can be represented at design-time using the XAML <Button> element. The Button class in C# represents the WPF button at run-time.

How do you edit a button in WPF?

I created a new button, edited the template of the button by removing everything within it. Then I added a grid into it and named it "grid". Inside the grid i added a textblock and called it "textBlock". Then i saved the template under Applications.

How do I close a window in WPF?

Pressing ALT + F4 . Pressing the Close button.

How do I close a window in C#?

When we need to exit or close opened form then we should use "this. Close( )" method to close the form on some button click event. When we are running a winform application & need to exit or close SUB APPLICATION or CURRENT THREAD then we should use "System. Windows.


2 Answers

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(); } 
like image 56
Steven Robbins Avatar answered Oct 06 '22 00:10

Steven Robbins


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();    } } 
like image 33
John Myczek Avatar answered Oct 06 '22 00:10

John Myczek