We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Programmatically cancel WPF window closing

Thread ID:





116529 Jun 5,2014 08:28 PM UTC Nov 26,2018 06:08 AM UTC WPF 4
Tags: DockingManager
James Cronen
Asked On June 5, 2014 08:28 PM UTC

When our users click the "X" on a Syncfusion DockingManager docked tab to close the window, I want to detect if the currently displayed document is dirty, and if so, ask the user if they want to save the document before closing. Standards dictate that the user should be able to cancel the closure at this point.

        private void DockingManager_DockStateChanging(FrameworkElement sender, DockStateChangingEventArgs e) {
            // This handles (and perhaps cancels) the window closure if there are unsaved changes to a 
            // drawing.

            if (e.TargetState == DockState.Hidden) {
                if (sender is DrawingWindow && (sender as DrawingWindow).IsDirty) {
                    bool cancelThis = ! (sender as DrawingWindow).ConfirmSaveBeforeClose();

                    e.Cancel = cancelThis;

        private void DockingManager_DockStateChanged(FrameworkElement sender, DockStateEventArgs e) {
            // This does the actual window closing.

The problem is that the window is closed BEFORE DockingManager_DockStateChanging is called. Therefore, e.Cancel, even if true, is too late, and the operation cannot be undone.

This seems like a bug to me. Alternately, if this is the designed behavior, how can I programmatically cancel a window closure? It appears that the Windows Forms DockingManager has a DockAllow event that might do this kind of thing; however this event does not exist in WPF.


Berkunath A [Syncfusion]
Replied On June 6, 2014 10:27 AM UTC

Hi James,


Thank you for contacting Syncfusion support.


You can handle DocumentWindow when you close the window using the DocumentClosing event of DocumentContainer and handle Dock and Float window when you close the window using the WindowClosing event of DockingManager. We have prepared the sample that tries to meet your requirement. In this sample we have cancel the DocumentClosing and WindowClosing.


You can download the sample from the following attachment.


Please let us know if it helps you.



Berkunath A

Attachment: DocumentWindowClosing_7405cd40.zip

James Cronen
Replied On June 9, 2014 09:09 PM UTC


Thanks, it appears that this works as specified.


Replied On November 24, 2018 11:13 AM UTC

it very simple and no need complication
click design and then click windows event from properties and then find  closing and double click 
and then write e.cancel=true it  work for C# WPF 

Kannan R [Syncfusion]
Replied On November 26, 2018 06:08 AM UTC

Hi Anil, 
Thank you for your suggestion.  


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon