Dock Panel is used to Dock WPF UI components, which means you have to define the relativity of each element to place on UI. Child components are docked in the left, right, top, and bottom locations of the relative elements using a Dock Panel.
The Dock property of each individual child element and the child elements’ relative order governs the location of the child elements. The dock property’s default value is still present. The Left, Right, Top and Bottom values of the Dock property’s enumeration of the same name are available.
Dock Panel is the 4th type of WPF Layout which we are discussing in this article after Wrap Panel.
Here I’m sharing an example with you guys to understand Dock. I added five buttons and defines the Dock Value as Top, Bottom, Left & Right. When we assign any value to Dock, a particular UI element is strict to follow then the UI placement.
Remember that all UI elements are associative with each other if their dock values are the same, which means if two buttons are using Top Dock then they will be placed on top of each other according to the line of code sorting order.
In the following code, you can observe the above Image.
Code:
<Window x:Class="JWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:JWPF" mc:Ignorable="d" Title="MainWindow" Height="250" Width="400"> <DockPanel> <Button DockPanel.Dock="Top">Button 1</Button> <Button DockPanel.Dock="Left">Button 2</Button> <Button DockPanel.Dock="Right">Button 3</Button> <Button DockPanel.Dock="Bottom">Button 4</Button> <Button>C# WPF Dock Panel Tutorial</Button> </DockPanel> </Window>
If you want to understand the Dock Panel concept in-depth you can watch the following video.