Exceptions when closing the document using the context menu close command

Jul 8, 2009 at 7:28 AM

First of all, please receive my congratulations for your work and the really great library.

I am using your library with the CompositeWPF  - Prism(see: http://www.codeplex.com/CompositeWPF) using the custom region adapter.

Anyway,  DocumentContent.Content is UserControl class i.e. my view.
When document is closed on right click context menu Close command I have the null reference exception because e.Parameter is not ManagedContent, it is UserControl.

I have a small proposal for the "correction" of the error (if applicable, if this is an "error" in the library and not in my custom region adapter):

Existing code (in DocumentPane.cs line 140):

public void ExecutedCloseCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            if (GetManager() == null)
                return;

            ManagedContent contentToClose = SelectedItem as ManagedContent;

            if (e.Parameter != null)
                contentToClose = e.Parameter as ManagedContent;

            DockableContent dockableContent = contentToClose as DockableContent;

            if (dockableContent != null)
                GetManager().Hide(dockableContent);
            else
            {
                DocumentContent documentContent = contentToClose as DocumentContent;
                documentContent.Close();

                //if (documentContent != null)
                //    Items.Remove(documentContent);

                //CheckContentsEmpty();
            }
        }

     Proposed solution:
        
        public void ExecutedCloseCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            if (GetManager() == null)
                return;

            ManagedContent contentToClose = SelectedItem as ManagedContent;

            if (e.Parameter != null && e.Parameter is ManagedContent)
                contentToClose = e.Parameter as ManagedContent;

            DockableContent dockableContent = contentToClose as DockableContent;

            if (dockableContent != null)
                GetManager().Hide(dockableContent);
            else
            {
                DocumentContent documentContent = contentToClose as DocumentContent;
                documentContent.Close();

                //if (documentContent != null)
                //    Items.Remove(documentContent);

                //CheckContentsEmpty();
            }
        }