Click here to Skip to main content
15,885,782 members
Articles / Desktop Programming / WPF

WPF: Close command on the Window close button

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
8 Sep 2009GPL3 51.9K   5   9
I could not find a good solution to this anywhere, so here is mine (which I think is a bit of an abuse of attached properties, but hey ho!).

Problem

I have an ExitApplication command that I want to execute when I click the ‘x’ on the main window. There does not seem to be any way to expose the Close button without overriding the control template of the window. I did not want to write code-behind even though in this case it may be the most pragmatic approach! Nonetheless, I was determined to find an alternative.

My solution (with a suggestion from a colleague) is to create an attached property for the command, and when the set property method is called, I could bind to the Closed event which would execute the attached command. And here is my code:

C#
public static class WindowAttachedProperties
{
    public static readonly DependencyProperty
            CloseCommand =
            DependencyProperty.RegisterAttached(
            "CloseCommand",
            typeof(ICommand),
            typeof(Window));

    public static ICommand GetCloseCommand(
        Window windowTarget)
    {
        throw new NotImplementedException(
        "The close command attached property "+
        "was not intended to be used outsite of the "+
        "closing even of a window.");
    }

    public static void SetCloseCommand(
       Window windowTarget, ICommand value)
    {
        // hack : because not setting property
        // just binding to an event!
        windowTarget.Closed += new EventHandler(
            (sender, args) =>
                    {
                        value.Execute(null);
                    });
    }
}

I would love to hear of a ‘better’ way of doing this!

This article was originally posted at http://www.sunmetablog.co.uk?p=178

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
United Kingdom United Kingdom
We cannot achieve perfection but we can strive for excellence. Excellence through creativity that pushes innovation, collaboration that facilitates productivity, knowledge that empowers people and persistence that eventually pays off !

Enjoy what you do and do what you enjoy !

Comments and Discussions

 
General[My vote of 1] Very poor license choice Pin
Nishant8428421-Oct-10 21:44
Nishant8428421-Oct-10 21:44 
GeneralRe: [My vote of 1] Very poor license choice Pin
Steve Psaltis13-Oct-10 2:43
Steve Psaltis13-Oct-10 2:43 
GeneralRe: [My vote of 1] Very poor license choice Pin
laduran10-Mar-11 10:41
laduran10-Mar-11 10:41 
I have to agree. GPL in general is considered viral where I work.

I suggest using the code project open license which is much more free to use in commercial work.
GeneralMy vote of 1 Pin
Giri Ganji14-Sep-09 23:11
Giri Ganji14-Sep-09 23:11 
GeneralRe: My vote of 1 Pin
Glenn Sandoval9-Aug-10 16:45
Glenn Sandoval9-Aug-10 16:45 
GeneralRe: My vote of 1 Pin
Steve Psaltis9-Aug-10 21:40
Steve Psaltis9-Aug-10 21:40 
GeneralRe: My vote of 1 Pin
Jesper Utoft23-Sep-10 3:30
Jesper Utoft23-Sep-10 3:30 
GeneralRe: My vote of 1 Pin
Steve Psaltis9-Oct-10 21:45
Steve Psaltis9-Oct-10 21:45 
GeneralRe: My vote of 1 Pin
Brian C Hart8-Aug-12 8:15
professionalBrian C Hart8-Aug-12 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.