Avalon Dock: Close Active Document


Overview

The following is an extension method that will close the active document in an AvalonDock WPF application.

/// <summary>
/// Closes the active document in the docking manager.
/// </summary>
/// <param name="dock"></param>
public static void CloseActiveDocument(this DockingManager dock)
{
    var docs = dock.Layout.Descendents().OfType<LayoutDocument>();
    LayoutDocument? activeDoc = docs.FirstOrDefault(layoutDocument => layoutDocument.IsActive);

    if (activeDoc != null)
    {
        var firstDocumentPane = dock.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
        firstDocumentPane?.Children.Remove(activeDoc);
    }
}