Click here to Skip to main content
Click here to Skip to main content

WPF: Close command on the Window close button

By , 8 Sep 2009
 

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:

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!

License

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

About the Author

Steve Psaltis
Software Developer
United Kingdom United Kingdom
Member
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 !

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General[My vote of 1] Very poor license choicememberNishant8428421 Oct '10 - 21:44 
GeneralRe: [My vote of 1] Very poor license choicememberSteve Psaltis13 Oct '10 - 2:43 
GeneralRe: [My vote of 1] Very poor license choicememberladuran10 Mar '11 - 10:41 
GeneralMy vote of 1memberGiri Ganji14 Sep '09 - 23:11 
GeneralRe: My vote of 1memberGlenn Sandoval9 Aug '10 - 16:45 
GeneralRe: My vote of 1memberSteve Psaltis9 Aug '10 - 21:40 
GeneralRe: My vote of 1memberJesper Utoft23 Sep '10 - 3:30 
GeneralRe: My vote of 1memberSteve Psaltis9 Oct '10 - 21:45 
GeneralRe: My vote of 1memberBrian C Hart8 Aug '12 - 8:15 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Steve Psaltis
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid