Home
WPF
DockPanel Example
Updated Sep 27, 2022
Dot Net Perls
DockPanel. A DockPanel helps align controls to edges. We add a control to it, and then can use the Dock property to align to the top, bottom, left or right.
Getting started. Add a Grid to a WPF Window, then a DockPanel and 3 nested buttons in it. I adjusted the HorizontalAlignment of the DockPanel to stretch.
Example code. I set the "Cat" button to be docked on the left. The middle button, "Dog," is left with the default: it sets against the Cat button.
And The third button, with the name "Squirrel," is docked to the right of the DockPanel.
Detail The DockPanel.Dock attribute handles four values: top, bottom, left and right. Only one can be specified.
<Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <DockPanel HorizontalAlignment="Stretch" LastChildFill="False" Margin="0" VerticalAlignment="Top"> <Button Content="Cat" Height="100" VerticalAlignment="Top" Width="75" DockPanel.Dock="Left"/> <Button Content="Dog" Height="100" VerticalAlignment="Top" Width="75"/> <Button Content="Squirrel" Height="100" VerticalAlignment="Top" Width="75" DockPanel.Dock="Right"/> </DockPanel> </Grid> </Window>
Window notes. The buttons are attached to their respective edges of the DockPanel. And if you run a program like this one, try resizing the Window (by dragging an edge).
Info The 3 Buttons will remain at the edges—this is a consistent visual effect.
Important The HorizontalAlignment property of the DockPanel makes the DockPanel expand to the window size.
Summary. WPF controls offer the ability to quickly construct useful programs. Many programs will have no need for a DockPanel. But this need occasionally arises.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 27, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen