#403 – The Order of DockPanel Children Matters

The layout and appearance of child elements in a DockPanel control is based not only on the Dock property of each child element, but on the order that they are listed in.  This is true because layout of a child is determined based on the available space left after all previous children have already been positioned.

In the example below, we add two Label controls at the top and bottom of the window and then add a StackPanel with buttons after that.

    <DockPanel LastChildFill="False">
        <Label Content="Some intro could go here, across the top" DockPanel.Dock="Top" Background="AliceBlue"/>
        <Label Content="Some pithy quote could go here, on the bottom" DockPanel.Dock="Bottom" Background="LightSteelBlue"/>

        <StackPanel DockPanel.Dock="Left" Background="Cornsilk">
            <Button Content="Open" Margin="5"/>
            <Button Content="Close" Margin="5"/>
            <Button Content="Save" Margin="5"/>
            <Button Content="Export" Margin="5"/>
        </StackPanel>
    </DockPanel>


If we move the StackPanel to be the first child of the DockPanel, it takes up the entire left edge of the window.