Destroy document windows on close based on a condition

Docking/MDI for WPF Forum

Posted 7 years ago by Eric P
Version: 16.1.0635
Avatar

I am 99% sure this does not exists and if it does not than this will quickly turn into an enhancement request. (or maybe I am looking at this all wrong) So I am aware of the DockSite.AreDocumentWindowsDestroyedOnClose property. This is an “all or nothing” approach, meaning all documents are destroyed or not destroyed. What about a situation where I need some documents of a specific type to be destroyed and other documents of a different type to just be hidden so it can be shown/opened later (like a ToolWinow)? I realize that I might be thinking about what should be a document vs. a tool window incorrectly, but without getting to that argument I just wanted to see if it was possible. My suggestion would be making it possible to bind a delegate to the DockSite (because we are MVVM) that would accept the Type of the DataContext of the DocumentWindow that is being closed and return a Boolean where true means destroy it and false means just hide it like a ToolWindow….or have something in the ViewModel that can be asked should it be destroyed or just hidden. It’s just a thought.

Comments (3)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Eric,

We don't have a conditional way built-in but it should be rather easy to implement this in your code. Set AreDocumentWindowsDestroyedOnClose to false so they don't destroy by default.  Then handle the WindowsClosed event and iterate e.Windows.  If you see one that should be destroyed, call its Destroy() method.  I believe that's all there is to it.


Actipro Software Support

Posted 7 years ago by Eric P
Avatar

BTW I am using verison 16.1.632.0. I am seeing mixed results with this approach. I set DockSite.AreDocumentWindowsDestroyedOnClose = true and I have the following in the code-behind.

private void dockSite_WindowClosed(object sender, DockingWindowsEventArgs e) {
    e.Handled = true;
    foreach (DockingWindow dockingWindow in e.Windows) {
        if (dockingWindow.DataContext is MyDynamicViewModel) {
            dockingWindow.Destroy();
        }
    }
}

There are a few ViewModel types that are hosted as a Document Window but lets just say there are two. One called MyDynamicViewModel (that should get destroyed when closed) and MyStaticViewModel (that does not get destroyed when closed, just hidden). I am uising DotMemory (from JetBrains) to make sure that the ViewModels are getting destroyed correctly. When I create an instance of one MyDynamicViewModel and then close its Document Window according to DotMemeory there is no references to MyDynamicViewModel after its closed. This is a good result. If I create one instance of MyDynamicViewModel and one instance of MyStaticViewModel after closing both according to DotMemory both remain in memory after being closed. This is not a good result. I would expect all instances of MyDynamicViewModel to be destryoed and the instance of MyStaticViewModel to remain. I fell like I am missing something simple. If you have any thoughts let me know.

Thanks,

   -eric

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You mentioned setting DockSite.AreDocumentWindowsDestroyedOnClose = true.  I assume that is a typo and you meant false.

What you have in the event handler looks correct though.  That's what I would do and should work.  If you see a specific problem that you think is a bug and you'd like us to debug, please make a new simple sample that repros the issue and email that over to our support address.  Rename the .zip file extension so it doesn't get spam blocked.  Thanks!


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.