Window.Close() from XAML

It’s been a long time since I last posted. Since then I have been working on my first WPF contract. I have definately drank the cool aid, I love WPF (when MVVM is being used anyway).

One thing I haven’t been able to find any info on is how to close a window or dialog from XAML. I tried and tried and then gave up. Until now!

Having discovered the power of Attached Behaviours, I decided to write one that would close a window. All you need do then is create a data trigger that watches a CloseSignal on the ViewModel and sets the Close property to true.

public static class WindowCloseBehaviour
public static void SetClose(DependencyObject target, bool value)
target.SetValue(CloseProperty, value);
public static readonly DependencyProperty CloseProperty =
new UIPropertyMetadata(false, OnClose));
private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
if (e.NewValue is bool && ((bool)e.NewValue))
Window window = GetWindow(sender);
if (window != null)
private static Window GetWindow(DependencyObject sender)
Window window = null;
if (sender is Window)
window = (Window)sender;
if (window == null)
window = Window.GetWindow(sender);
return window;

and then in your XAML

   <DataTrigger Binding="{Binding CloseSignal}" Value="true">
        <Setter Property="Behaviours:WindowCloseBehaviour.Close" Value="true" />
Tagged , ,

21 thoughts on “Window.Close() from XAML

  1. Mark says:

    Thanks, I got it working with a bit of googling.

    In VS2008 the compiler picked up a few errors with the code…

    Namely, I had to change the GetClose method to return a bool and not be a void.

    Also, for those that are noobs to WPF like me

    is an example of how to set the Attached behaviour.

    Thanks a ton though, I think this is the most elegant way I have found so far!

  2. JH says:

    Me = newb.

    I am getting an error “Cannot convert the value in attribute ‘Property’ to object of type ‘System.Windows.DependencyProperty’.” and am uncertain how to resolve it. Any guidance is appreciated.

  3. Jose Rodriguez says:

    I struggled with this code. the only way I could make it work is by removing ‘readonly’ from CloseProperty. Hope it helps. No other changes were necessary.

  4. […] it, despite perfectly answering the question — pointed to a blog post by Adam Mills: “Window.Close() from XAML”. Adam’s solution uses an attached behavior. I’m learning to appreciate the […]

  5. […] I have posted previously about one way to Close a Dialog from a ViewModel […]

  6. Michele says:

    slight syntax error in your code sample above – the xaml should read
    , not . thanks for this, very helpful.

    • hello says:

      Really it is helpful i dont think so . 🙂 feeling bad still i m searching the good answer . but good effort

  7. Michele says:

    well that didn’t post correctly… what i meant to say was:
    </Style.Triggers>, not </Style>

  8. Immolit says:

    I was stuck a lot of time to make this code work so I add some precisions :

    – You have to add : xmlns:Behaviours=”clr-namespace:namesapceOfWindowsCloseBehaviour”> at the top of the xaml file

    – Don’t forget to call your triggers
    ex :

    – Bind CloseSignal like in this code :

  9. Immolit says:

    I can’t post code… to call your triggers proceed like this :
    – put the style.triggers code in Style x:key=”StyleMainGrid” for example
    -the grid balise : Grid Style=”{StaticRessource StyleMainGrid}”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: