Click here to Skip to main content
14,219,408 members

WPF Docking Library

Rate this:
4.78 (79 votes)
Please Sign up or sign in to vote.
4.78 (79 votes)
17 Jul 2007CPOL
A WPF library to easily integrate Windows docking features in applications like VS

Screenshot - ScrShot1.gif


Recently, I started a project for porting a Windows Forms application to WPF. I was quite a novice in WPF, so at first I was considering using some type of Windows Forms interoperability. In particular, the WinForm application has cool docking functionalities that I wanted to port to a newer version. As I was going deeper into WPF technology, I discovered a new world that radically changed my initial ideas. In this article, I wish to share a library that implements the Windows docking feature in pure WPF without any Win32-interop.

Using the code

There are three fundamental classes: DockManager, DockableContent and DocumentContent. DockManager is a class responsible for managing the main window layout. Pane represents the window area that can be docked to a border. Each Pane contains one or more ManagedContent elements, which precisely refer to a window content element of the client code. Using this library is straightforward. DockManager is a user control that can be easily embedded into a window. For example, the following XAML code creates a DockManager in a DockPanel:

<Window x:Class="DockingDemo.MainWindow"



    Title="DockingDemo" Height="500" Width="500"


    Loaded="OnLoaded" Background="LightGray" >
        <Menu DockPanel.Dock="Top">
                        <MenuItem Header="File">
                <MenuItem Header="New" Click="NewDocument"/>
                <MenuItem Header="Exit"  Click="ExitApplication"/>
            <MenuItem Header="Edit"/>
            <MenuItem Header="Window">
                <MenuItem Header="Explorer" Click="ShowExplorerWindow"/>
                <MenuItem Header="Output"  Click="ShowOutputWindow"/>
                <MenuItem Header="Property" Click="ShowPropertyWindow"/>
                <MenuItem Header="ToDoList"  Click="ShowListWindow"/>
            <MenuItem Header="?"/>
        <ToolBar DockPanel.Dock="Top">
        <custom:DockManager Name ="DockManager"/>

Notice that to use DockManager you have to refer an external CLR namespace, DockingLibary here. You can now add your windows to DockManager with code like this:

public partial class MainWindow : System.Windows.Window
    private TreeViewWindow explorerWindow = new TreeViewWindow();
    private OutputWindow outputWindow = new OutputWindow();
    private PropertyWindow propertyWindow = new PropertyWindow();
    private ListViewWindow listWindow = new ListViewWindow();

    public MainWindow()

    private void OnLoaded(object sender, EventArgs e)
        //set window parent for dragging operations
        dockManager.ParentWindow = this;

        //Show PropertyWindow docked to the top border
        propertyWindow.DockManager = dockManager;

        //Show ExplorerWindow docked to the right border as default
        explorerWindow.DockManager = dockManager;

        //Show ListWindow in documents pane
        listWindow.DockManager = dockManager;

Ib order to be dockable, a window must derive from DockableContent. Deriving from DockableContent indicates that the window content can be hosted in DockablePane. Windows are initially docked either where you set the Show member call or, by default, to the left border. The last thing to see is how to add a document window. A document window is a particular window that can't be docked to the main window border. It lives only in DocumentsPane which, as DockablePane, is a particular kind of Pane. The following code adds a document window with a unique title to DockManager:

private bool ContainsDocument(string docTitle)
    foreach (DockingLibrary.DocumentContent doc in DockManager.Documents)
    if (string.CompareOrdinal(doc.Title, docTitle) == 0)
    return true;
    return false;

private void NewDocument(object sender, EventArgs e)
    string title = "Document";
    int i = 1;
    while (ContainsDocument(title + i.ToString()))

    DocumentWindow doc = new DocumentWindow();
    doc.Title = title+i.ToString();

Points of interest

Exactly how is docking realized? I implement a simple algorithm here to manage relations between windows that are docked. DockingGrid contains code to organize Pane in a logical tree. DockManager calls DockingGrid.ArrangeLayout in order to organize the window layout. You also use DockingGrid.ChangeDock when you need to dock a Pane to a main window border. The following image shows a logical tree of panes. Don't get confused with the WPF logical tree.

Screenshot - DockingLibTree.jpg

Each node is a group of either two Panes or a Pane and a child. To arrange the layout, DockingGrid creates a WPF grid for each group with two columns or two rows, depending on split orientation. I hope the image is self-explanatory. Anyway, feel free to ask for more details if you are interested.

From version 0.1, the library supports floating windows, as you can see in VS. A floating window is an instance of the FloatingWindow class. It' s a very particular window because it needs to "float" between two windows. One is the parent window, MainWindow in this case, which is usually the main application window. The other is a transparent window owned by FloatingWindow itself, which is shown during dragging operations.

Screenshot - ScrShot2.gif

As you can see in the previous image, FloatingWindow supports useful switching options through a context menu on the title bar. In WinForms, controlling a non-client window area means overriding WndProc and managing relevant messages like WM_NCMOUSEMOVE.

In WPF, we have no access to messages posted to a window. This is because everything is controlled by the WPF engine that draws window content, fires keyboard and mouse events and does a lot of other things. The only way I know of to intercept messages is to install HwndSourceHook with a delegate to our functions. The following code shows how to manage a WM_NCLBUTTONDOWN message:

protected void OnLoaded(object sender, EventArgs e)
    WindowInteropHelper helper = new WindowInteropHelper(this);
    _hwndSource = HwndSource.FromHwnd(helper.Handle);
    _hwndSource.AddHook(new HwndSourceHook(this.HookHandler));

private IntPtr HookHandler(
    IntPtr hwnd,
    int msg,
    IntPtr wParam,
    IntPtr lParam,
    ref bool handled
    handled = false;

        case WM_NCLBUTTONDOWN:
        if (HostedPane.State == PaneState.DockableWindow && 
            wParam.ToInt32() == HTCAPTION)
            short x = (short)((lParam.ToInt32() & 0xFFFF));
            short y = (short)((lParam.ToInt32() >> 16));

                this, new Point(x, y), new Point(x - Left, y - Top));

            handled = true;}

Although the delegate signature looks like a window procedure signature, it's not the same thing. You can, however, handle every message, even WM_PAINT.


  • 13/05/07 -- First preliminary release
  • 17/05/07 -- Update
  • 11/06/07 -- Version 0.1: Floating window, many improvements and bug fixes
  • 16/07/07 -- Version 0.1.1: Some annoying bug fixes


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Systems Engineer
Italy Italy
I bought my first computer in Nov 1991, 21st and I started programming with QBasic under MSDOS.
Today my main interest is developping applications with .NET and HTML5 stack.

Comments and Discussions

AnswerRe: How to get DockableContents Pin
azamat4-Jun-08 0:18
memberazamat4-Jun-08 0:18 
GeneralDockableContent ShowAsFloating Pin
azamat3-Jun-08 22:19
memberazamat3-Jun-08 22:19 
QuestionCan we convert this to windows forms? Pin
Ceaser Y1-Jun-08 23:46
memberCeaser Y1-Jun-08 23:46 
AnswerRe: Can we convert this to windows forms? Pin
adospace.NET2-Jun-08 22:53
memberadospace.NET2-Jun-08 22:53 
GeneralInvalidOperationException while using DockingLibrary. Pin
azamat1-Jun-08 23:37
memberazamat1-Jun-08 23:37 
GeneralRe: InvalidOperationException while using DockingLibrary. Pin
BorodaAlex28-Oct-09 4:55
memberBorodaAlex28-Oct-09 4:55 
GeneralAvalon. Window title context menu items. Pin
azamat28-May-08 23:58
memberazamat28-May-08 23:58 
GeneralRe: Avalon. Window title context menu items. Pin
adospace.NET29-May-08 1:04
memberadospace.NET29-May-08 1:04 
Thank you azamat.

Here some explanations:
1. A floating windows has two states: Floating and Dockable. If click Floating menu item from context menu which appears when you click on floating window title you select the Floating state, otherwise if you click Dockable menu item you select the Dockable state. When a floating window is in Floating state you cannot redock the window contents to the original docking manager, this means that if you move the window the docking manager doesn't show the anchor icons. (as happens in VS)
2/3. Context menu on a pane when is in AutoHide state has not the right enabled/disabled states because the OnCanExecute handlers are not properly compiled at this time, even if the commands like FloatingWindow have no effect. This issue will be fixed in next release.

Regarding the option to mantains all the context menus enabled, I disagree. In my opinion commands should be enabled/visible only if commands have effect in that context.

For further discussions on AvalonDock please use the discussion section on CodePlex site, and to report a bug like point 3 use the issue tracker section. In this way other users of the library can see the problems and you can ckeck if the bug has been fixed in a particular release.
GeneralRe: Avalon. Window title context menu items. Pin
azamat29-May-08 1:28
memberazamat29-May-08 1:28 
GeneralRe: Avalon. Window title context menu items. Pin
adospace.NET29-May-08 2:38
memberadospace.NET29-May-08 2:38 
GeneralRe: Avalon. Window title context menu items. Pin
Vivek_Minhas9-May-10 22:53
memberVivek_Minhas9-May-10 22:53 
GeneralInvalid character in the given encoding. Pin
azamat28-May-08 0:25
memberazamat28-May-08 0:25 
GeneralPlacement and resize Pin
amondel7-May-08 22:14
memberamondel7-May-08 22:14 
GeneralRe: Placement and resize Pin
adospace.NET7-May-08 22:32
memberadospace.NET7-May-08 22:32 
GeneralRe: Placement and resize Pin
amondel9-May-08 6:57
memberamondel9-May-08 6:57 
GeneralRe: Placement and resize Pin
amondel12-May-08 4:33
memberamondel12-May-08 4:33 
GeneralRe: Placement and resize Pin
adospace.NET12-May-08 4:52
memberadospace.NET12-May-08 4:52 
GeneralAvalonDock is published! Pin
adospace.NET29-Apr-08 14:17
memberadospace.NET29-Apr-08 14:17 
GeneralFeature requests Pin
Derek Bartram12-Apr-08 15:12
memberDerek Bartram12-Apr-08 15:12 
GeneralRe: Feature requests Pin
taher bayat13-Feb-09 10:36
membertaher bayat13-Feb-09 10:36 
GeneralUnDock Pin
azamat6-Apr-08 18:03
memberazamat6-Apr-08 18:03 
GeneralRe: UnDock Pin
adospace.NET6-Apr-08 22:00
memberadospace.NET6-Apr-08 22:00 
GeneralCannot get the docking function to work. Pin
John Willcox2-Apr-08 3:18
memberJohn Willcox2-Apr-08 3:18 
GeneralNew run time error Pin
John Willcox2-Apr-08 3:16
memberJohn Willcox2-Apr-08 3:16 
GeneralCrash when run demo Pin
John Willcox26-Mar-08 12:53
memberJohn Willcox26-Mar-08 12:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 15 May 2007


310 bookmarked