Docking controls using the DockPanel
DockPanel
makes it easier to dock UI elements in the left, right, top, or bottom of the screen. This is often useful, mainly when you want to divide the window into specific areas. For example, a status bar is always kept at the bottom of the window, whereas a menu or a toolbar resides at the topmost position of the window.
This recipe will help you to learn how to dock child elements in an application window.
Getting ready
Let's begin with a new project. Open Visual Studio and create a project named CH03.DockPanelDemo
, based on the available WPF application template.
How to do it...
Perform the following steps to add a DockPanel
with a few labels docked into it:
- From
Solution Explorer
, navigate to the project and openMainWindow.xaml
. - Replace the existing
Grid
panel with aDockPanel
control. - Now add five labels inside it and dock them in various sides of the window.
- Here's the complete XAML code for reference:
<DockPanel> <Label Content="Button ...