WPF Standard Control Demo App › DockPanel

DockPanel Layout

DockPanel places each child along one of its edges, in order, and by default gives the last child the space that is left.

Children take their edge in the order they are written

In a DockPanel, each child sets the attached DockPanel.Dock; a child without it is docked Left. Children take their edge in the order they are written, from the space the earlier children left. In a panel 300 × 100 with a top, a bottom, a left, and a content child, the left child was 44.08 high, between the top and the bottom. Written first, the same left child took the full 100 of the height. So write the children in the order they should take space: a side pane written before the top and bottom bars runs the full height of the window.

The edge decides which dimension a child keeps. In a panel 300 × 100, Left and Right gave a label its own width and the full height, at x=0 and x=257.66. Top and Bottom gave it the full width and its own height, 27.96, at y=0 and y=72.04.

What LastChildFill does to the last child

LastChildFill is True by default. The last child then fills the rest, and its own Dock is ignored: a last child with Dock="Top" still filled the remaining 257.66 × 100 beside the first child. Leave Dock off the last child in that case, since it has no effect.

Set LastChildFill="False" to dock every child to an edge; the remaining space then stays empty. With two labels in a panel 300 wide, False left the second label at its own width, 42.34, and True stretched it to 257.66. The demo app binds LastChildFill to a check box that starts unchecked, so the demo starts at False, not at the default True.

Overlap and the empty area

With LastChildFill="False" and one child on the left, a hit test in the empty area found nothing with the default Background of null, and found the panel once it had a color. Give the panel a Background if the empty space should receive clicks.

Where children overlap, Panel.ZIndex decides which one is on top. In the demo app, the second label has a left margin of -30 and overlaps the first. With ZIndex 1 and 2, the second label was on top at the overlap; with 3 and 2, the first.

case measured
defaults: LastChildFill / Dock of a child True / Left
demo's two labels, LastChildFill=False: Item1 / Item2 x=0 y=0 w=42.34 h=100 / x=42.34 y=0 w=42.34 h=100
demo's two labels, LastChildFill=True: Item1 / Item2 x=0 y=0 w=42.34 h=100 / x=42.34 y=0 w=257.66 h=100
LastChildFill=True, last label with Dock=Top: its rect x=42.34 y=0 w=257.66 h=100
demo's single label, LastChildFill=False, Dock=Left x=0 y=0 w=42.34 h=100
demo's single label, LastChildFill=False, Dock=Top x=0 y=0 w=300 h=27.96
demo's single label, LastChildFill=False, Dock=Right x=257.66 y=0 w=42.34 h=100
demo's single label, LastChildFill=False, Dock=Bottom x=0 y=72.04 w=300 h=27.96
Top, Bottom, Left, content: Left / content x=0 y=27.96 w=31.75 h=44.08 / x=31.75 y=27.96 w=268.25 h=44.08
Left, Top, Bottom, content: Left / content x=0 y=0 w=31.75 h=100 / x=31.75 y=27.96 w=268.25 h=44.08
LastChildFill=False, Background=null: hit test in the empty area (nothing)
LastChildFill=False, Background=AliceBlue: hit test in the empty area Panel
demo's ZIndex labels (Item2 margin -30), ZIndex 1 and 2: on top where they overlap Item2
demo's ZIndex labels (Item2 margin -30), ZIndex 3 and 2: on top where they overlap Item1

Docking, LastChildFill, the order of children, background, and ZIndex. Measured on .NET 10 / Windows 11.

Trying it in the demo app

The DockPanel page of the demo app, with the control list on the left and the first section, LastChildFill

The DockPanel page of the demo app has sections for LastChildFill, Dock, Background, and ZIndex. The “Show Code” link under each section displays its XAML. The following XAML is the Dock section (DockPanelUsageControl.xaml), with the styles and the surrounding GroupBoxes left out and the namespace declarations added. markupextensions is the prefix for the demo app’s own markup extensions:

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:markupextensions="clr-namespace:WPFStandardControlDemoApp.Common.MarkupExtensions">
  <ComboBox x:Name="DockComboBox"
            DisplayMemberPath="Name"
            ItemsSource="{markupextensions:EnumBindingSource EnumType=Dock}"
            SelectedValuePath="Value"
            SelectedIndex="0" />

  <DockPanel x:Name="DockUsageDockPanel" Height="100" LastChildFill="False">
    <Label Background="LightBlue" BorderBrush="Black" BorderThickness="1"
           Content="Item1"
           DockPanel.Dock="{Binding SelectedValue, ElementName=DockComboBox}" />
  </DockPanel>
</StackPanel>

Source code and how it was measured

Every behavior on this page was measured by running it on .NET 10 / Windows 11, using DockPanelDemoScene in this site’s screenshot tool, with labels like the demo app’s (border 1) in a panel 300 × 100. The panels were laid out with Measure and Arrange. Sizes are the values on the measuring machine.

View DockPanel source code on GitHub →