Home » WPF DockPanel Layout

WPF DockPanel Layout

by Online Tutorials Library

WPF DockPanel Layout

DockPanel gives us an area to arrange the elements with each other. We can place the child elements either horizontally or vertically.

With the help of the DockPanel, we can easily arrange the child elements at the top, bottom, right, left, and the center by using the Dock property.

DockPanel is used to place the content in all the directions. We can divide the window into a specific area. We will use a DockPanel.Dock property to decide which direction we want to use to place the child control. In case if we didn’t use this, then the first control will be placed to the left. The last one will cover all the space.

Here we are going to take an example to place the elements in the screen according to the dock panel layout as shown below:

MainWindow.XAML

The output of the above code will look like as shown in the below screenshot:

OUTPUT

WPF DockPanel Layout

In the above example, if we notice that we assigned the dock position for the child element. The last child element automatically acquires the whole remaining space. The controls around the center will take the space according to their need, and the remaining space is left for the last child element. The Right child element acquires the space more than the Left child control. It is just because of the extra character is added in the right child element, and the extra character will need the extra space.

Now we will talk about how the space of the screen is divided. Here if we notice that the top element doesn’t acquire all the top space. It was just because the left button covers the part of the top button. In this situation, DockPanel will decide the space of the controls by looking at their mark-up.

In this case, the left button precedes because this button is placed in the first position in the mark-up. We can easily change the controls’ space by assigning the heights and widths to the child control.

Here we will see another example where we will set the height and width of the control. For this we will write the below code:

MainWindow.XAML

The output of the code is shown us like as in the below screenshot:

OUTPUT

WPF DockPanel Layout

Here in the above screenshot we can see that the both the controls like top and bottom precede over the left and right controls. When we resize the window either in smaller or bigger, in that case we will see that the static width and height will remain the same.

When we resize the screen of the window, only the centre area will increase or decrease.

LastChildFill

As we know that by default the last child of the DockPanel acquires the rest of the space. But we can disable this behaviour by using the LastChildFill.

Here we will take an example, where we will write a code to disable the last child element of the dock and will also show the behaviour of the dock of having more than one control at the same side.

For that we will write the below XAML code:

MainWindow.XAML

The output of the above code will be like as shown in the below screenshot:

OUTPUT

WPF DockPanel Layout

Here in the above example, we place the two controls on the left and the two controls on the right and disable the LastChildFill property. Then through this we get the empty space in the centre which is in demand in most of the cases.

Here we will take another example of the DockPanel. Here we will add the button in the DockPanel with the help of the XAML code:

MainWindow.XAML

Now we will do the C# coding on the button click event.

MainWindow.cs

The output of the above code will be like as shown in the below screenshot:

OUTPUT

WPF DockPanel Layout


Next TopicWPF Canvas Panel

You may also like