Programmatically close a dockable content

Jan 12, 2009 at 8:48 AM
I programatically create different DockableContent windows at certain points in my application. Upon closing said windows (clicking their close 'X' button), I would like them to actually close (not just hide) so that they are garbage collected and free their resources. I've manage to get the programmatic window createtion step functional, but am at a loss on how to get them to actually close. How should I go about doing that?
Jan 12, 2009 at 7:16 PM
Hi, DockableContents are supposed to be always alive in AvalonDock so I'm afraid that there is not an easy way to get them closed insted of simply hidden. Anyway you can try to use commandbinding (without the need of restyling).

In you class deriving from a dockablecontent:

<pseudo-ccode>

protected override OnInitialized(EventArgs e)
{
            this.CommandBindings.Add(
                new CommandBinding(HideCommand, this.OnExecuteHideCommand, this.OnCanExecuteHideCommand));
    
    //call base method
    base.OnInitialized(e)
}

void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
{
    //remove myself from mycontainer
    ContainerPane.Remove(this);
    e.Handled  = true;
}

Hope it helps,
Ado
Jan 14, 2009 at 3:42 AM
So essentially, if I can find the Pane that holds the DockableContent and call Remove() things should work out? Is there a chance of leaving around a bunch of Panes with nothing in them?
Jan 16, 2009 at 8:01 AM
Edited Jan 16, 2009 at 8:03 AM
After trying a bunch of non-invasive methods, I concluded there was no way getting around my DockableContent windows from being tracked in the _hiddenContents list. Because of this I ended up making a couple small hacks to get the results I needed and wanted to share them incase anyone else needs the same.

In the class DockableContent, I added the following virtual proprty that my derived classes can override to enable the ability to get deleted.
// JUCKETT: Added this property to support closable dockable content windows
public virtual bool DeleteOnHide { get { return false; } }

In the function DockingManager.Hide(DockableContent content), I added the following right before the _hiddenContents.Add(content) line to prevent the hidden content from being tracked and thus eventually being garbage collected.

// JUCKETT: Added this check to support closable dockable content windows
if( !content.DeleteOnHide )
    return;
As far as I can tell, this is functioning fine (let me know if I missed something) and while it doesn't function in the most intuitive manner (we are handling our "close" behaviour inside a function called "Hide"), it is a simple odification which works well for tracking your changes to third party libraries.