Why does DockPanel.Dock="Bottom" put element at the top?

Edward Tanguay picture Edward Tanguay · Jun 17, 2009 · Viewed 9.8k times · Source

The following code puts the two text elements at the top even though the second one is marked "Bottom". The background color goes all the way to the bottom so the DockPanel seems to stretch to the bottom.

What am I not understanding about DockPanel?

<Window x:Class="TestIndexer934.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestIndexer934.Commands"
    Title="Main Window" Height="400" Width="800">
    <DockPanel HorizontalAlignment="Left" Background="Beige">
        <TextBlock DockPanel.Dock="Top" Text="Testing top"/>
        <TextBlock DockPanel.Dock="Bottom" Text="Testing bottom"/>
    </DockPanel>
</Window>

Answer

rmoore picture rmoore · Jun 17, 2009

By default a DockPanel's last item will fill the remaining content area available.

If you set LastChildFill="False" on the DockPanel, you'll see the behavior you are expecting. You can also set VerticalAlignment="Bottom" on the TextBlock.