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. Image for the cookie policy date
close icon

Programmatically cancel WPF window closing

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.

Thanks.

4 Replies

BA Berkunath A Syncfusion Team 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.

 

Regards,

Berkunath A


Attachment: DocumentWindowClosing_7405cd40.zip


JC James Cronen June 9, 2014 09:09 PM UTC

Berkunath,

Thanks, it appears that this works as specified.

Cheers,
jim.


AA ANIL AYDINALP 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 


KR Kannan R Syncfusion Team November 26, 2018 06:08 AM UTC

Hi Anil, 
 
Thank you for your suggestion.  
 
Regards, 
Kannan 


Loader.
Live Chat Icon For mobile
Up arrow icon