Share via


How to: Dock a Control Using DockPanel

When developing user interfaces, you will frequently want to dock a control to a side of the window. For example menu bars are frequently docked to the top or side of a window. You can use the DockPanel container in Windows Presentation Foundation (WPF) to dock controls to the sides of the window.

To dock a control in a DockPanel

  1. If you do not already have a DockPanel in your user interface, drag a DockPanel from the Toolbox onto the WPF Designer.

  2. Drag the control that you want to dock onto the DockPanel.

  3. In the Properties window, set the control's DockPanel.Dock property to the side of the DockPanel you want to dock it to. For example, if you want to dock the control to the top of the DockPanel, set this property to Top.

  4. If the Dockstyle.Dock property is set to Top or Bottom, set the control's Width property to Auto. Likewise, if the Dockstyle.Dock property is set to Left or Right, set the controls Height property to Auto.

    Note that the DockPanel is set to LastChildFill by default, which means that the last child control added to the DockPanel will have its DockPanel.Dock property set to Fill. To change this behavior, set the DockPanel's LastChildFill property.

See Also

Concepts

WPF Container Controls Overview

Other Resources

WPF Container Controls

DockPanel

DockPanel Samples