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.
Unfortunately, activation email could not send to your email. Please try again.

Programmatically cancel WPF window closing

Thread ID:

Created:

Updated:

Platform:

Replies:

116529 Jun 5,2014 04:28 PM Jun 9,2014 05:09 PM WPF 2
loading
Tags: DockingManager
James Cronen
Asked On June 5, 2014 04:28 PM

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.

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

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

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

Berkunath,

Thanks, it appears that this works as specified.

Cheers,
jim.

CONFIRMATION

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.

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.

;