Search code examples
c#avalondock

Avalon Dock Document Content Save Before Closing C#


I am using Avalon Dock in a project that's similar to notepad++ i have the documents as Document content, the problem that im facing is that when a user closes a document content

i want him to get a pop up msg if he/she wants to save the document before closing.

How can i accomplish this?

Thank you in advance.


Solution

  • In the version of AvalonDock I'm using (1.2.2668.0), I can simply define a handler for the Closing event of a DocumentContent.

    example for adding a handler to a DocumentContent, I am adding a handler to the Closing event:

    DocumentContent dc = new DocumentContent();
    dc.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(dc_Closing);
    

    The sample handler:

        void dc_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult Res = MessageBox.Show("Do you want to save?", "Save document?", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
            switch (Res)
            {
                case MessageBoxResult.Cancel:
                    //User cancelled, he probably doesn't want to close the window
                    e.Cancel = true; 
                    break;
                case MessageBoxResult.No:
                    //Nothing to do - continue closing
                    e.Cancel = false;
                    break;
                case MessageBoxResult.Yes:
                    //He does want to save - launch save here!
                    Save_Function_For_DocumentContent(sender);
                    e.Cancel = false;
                    break;
                default:
                    //Something unexpected, better abort
                    e.Cancel = true; 
                    break;
            }
    

    Hope this helps, if not, which version of AvalonDock are you using? }