Dock Panel Control In UWP

The DockPanel Control defines an area where you can arrange child elements either horizontally or vertically, relative to each other. The DockPanel position child controls based on the child Dock property, we have 4 options to Dock, left (Default), right, top, bottom. We can set DockPanel LastChildFill property to true if we want the last item added to the DockPanel to fill the rest of the empty space.

DockPanel Control is from UWP Community Toolkit. The UWP Community Toolkit is a collection of helper functions, custom controls, and app services and it simplifies and demonstrates common developer tasks building UWP apps for Windows 10.

Reading this article, you can learn how to use DockPanel Control in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing UWP,

Step 1

Open Visual studio 2017 -> Start -> New Project -> Windows Universal (under Visual C#) -> Blank App(Universal Windows) -> Give a suitable name for your App (UWPDockPanel) -> OK.

 

After choosing the target and minimum platform version that your Windows Universal Application will support (Windows Anniversary Update (10.0.14393.0)), the project creates App.xaml and MainPage.xaml.

 

Step 2

First, update the reference to Microsoft.NETCore.UniversalWindowsPlatform with the latest version using "Manage NuGet Packages".

Step 3

Add the following controls in design window for DockPanel control view. Add the TextBlock and change the name and text properties.

Add the DockPanel control and set the name and LastChildFill properties.

  1. <Controls:DockPanel Name="DPtest" Margin="9,66,3,2" Background="LightGray" LastChildFill="false" />  
 

Add images to the Assets folder for DockPanel Demo.

 

For DockPanel Top, add image control and set the height, source, and DockPanel.Dock properties.

  1. <Image Height="100" Controls:DockPanel.Dock="Top" Source="Assets/Border.jpg" Stretch="Fill"/>  
 

Similarly, set DockPanel Left, Bottom and Right with Image controls.

  1. <Image Width="100" Controls:DockPanel.Dock="Left" Source="Assets/Borderleft.png" Stretch="Fill" />  
  2. <Image Height="100" Controls:DockPanel.Dock="Bottom" Source="Assets/Border.jpg" Stretch="Fill" />  
  3. <Image Width="100" Controls:DockPanel.Dock="Right" Source="Assets/Borderleft.png" Stretch="Fill" />  
 

Also, add stackpanel with textbox control for adding more controls in DockPanel Top, Left, Bottom, and Right.

  1. <StackPanel Controls:DockPanel.Dock="Top" Height="44" Background="LightCyan" VerticalAlignment="Center" Orientation="Vertical" Margin="0,0,0,56" >  
  2.    <TextBlock TextAlignment="Center" Height="30" TextWrapping="Wrap" FontSize="20" Text="Top Dock Panel" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="10,0" />  
  3. </StackPanel>  
  1. <StackPanel Controls:DockPanel.Dock="Left" Width="60" Background="Aqua" VerticalAlignment="Center" Height="393" Margin="0,-51,0,64">  
  2.     <TextBlock TextAlignment="Center" TextWrapping="Wrap" FontSize="20" VerticalAlignment="Bottom" Foreground="#FFD8471C" Height="278">  
  3.         <Run/>  
  4.         <LineBreak/>  
  5.         <Run/>  
  6.         <LineBreak/>  
  7.         <Run/>  
  8.         <LineBreak/>  
  9.         <Run/>  
  10.         <LineBreak/>  
  11.         <Run/>  
  12.         <LineBreak/>  
  13.         <Run/>  
  14.         <LineBreak/>  
  15.         <Run Text="Left Dock Panel" /> </TextBlock>  
  16. </StackPanel>  
  17. <StackPanel Controls:DockPanel.Dock="Bottom" Height="60" Background="Yellow" VerticalAlignment="Center" Orientation="Vertical" Margin="-55,0,0,0">  
  18.     <TextBlock TextAlignment="Center" Height="60" TextWrapping="Wrap" FontSize="20" Text="Bottom Dock Panel" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="0,0,10,0" /> </StackPanel>  
  19. <StackPanel Controls:DockPanel.Dock="Right" Width="60" Background="LawnGreen" VerticalAlignment="Center" Orientation="Vertical" Height="397" Margin="0,-51,0,0">  
  20.     <TextBlock TextAlignment="Center" Height="322" TextWrapping="Wrap" FontSize="20" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="10,0,0,0">  
  21.         <Run/>  
  22.         <LineBreak/>  
  23.         <Run/>  
  24.         <LineBreak/>  
  25.         <Run/>  
  26.         <LineBreak/>  
  27.         <Run/>  
  28.         <LineBreak/>  
  29.         <Run/>  
  30.         <LineBreak/>  
  31.         <Run/>  
  32.         <LineBreak/>  
  33.         <Run Text="Right Dock Panel" /> </TextBlock>  
  34. </StackPanel>  
 

Note

Automatically, the following code will be generated in XAML code view while we are done in the design view.

  1. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPDockPanel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls" x:Class="UWPDockPanel.MainPage" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,10,612,216">  
  3.         <TextBlock x:Name="tbltitle" HorizontalAlignment="Left" Margin="274,10,0,0" TextWrapping="Wrap" Text="Dock Panel Control in UWP" VerticalAlignment="Top" Height="56" Width="316" FontSize="24" FontWeight="Bold" />  
  4.         <Controls:DockPanel Name="DPtest" Margin="9,66,3,2" Background="LightGray" LastChildFill="false">  
  5.             <Image Height="100" Controls:DockPanel.Dock="Top" Source="Assets/Border.jpg" Stretch="Fill" />  
  6.             <Image Width="100" Controls:DockPanel.Dock="Left" Source="Assets/Borderleft.png" Stretch="Fill" />  
  7.             <Image Height="100" Controls:DockPanel.Dock="Bottom" Source="Assets/Border.jpg" Stretch="Fill" />  
  8.             <Image Width="100" Controls:DockPanel.Dock="Right" Source="Assets/Borderleft.png" Stretch="Fill" />  
  9.             <StackPanel Controls:DockPanel.Dock="Top" Height="44" Background="LightCyan" VerticalAlignment="Center" Orientation="Vertical" Margin="0,0,0,56">  
  10.                 <TextBlock TextAlignment="Center" Height="30" TextWrapping="Wrap" FontSize="20" Text="Top Dock Panel" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="10,0" /> </StackPanel>  
  11.             <StackPanel Controls:DockPanel.Dock="Left" Width="60" Background="Aqua" VerticalAlignment="Center" Height="393" Margin="0,-51,0,64">  
  12.                 <TextBlock TextAlignment="Center" TextWrapping="Wrap" FontSize="20" VerticalAlignment="Bottom" Foreground="#FFD8471C" Height="278">  
  13.                     <Run/>  
  14.                     <LineBreak/>  
  15.                     <Run/>  
  16.                     <LineBreak/>  
  17.                     <Run/>  
  18.                     <LineBreak/>  
  19.                     <Run/>  
  20.                     <LineBreak/>  
  21.                     <Run/>  
  22.                     <LineBreak/>  
  23.                     <Run/>  
  24.                     <LineBreak/>  
  25.                     <Run Text="Left Dock Panel" /> </TextBlock>  
  26.             </StackPanel>  
  27.             <StackPanel Controls:DockPanel.Dock="Bottom" Height="60" Background="Yellow" VerticalAlignment="Center" Orientation="Vertical" Margin="-55,0,0,0">  
  28.                 <TextBlock TextAlignment="Center" Height="60" TextWrapping="Wrap" FontSize="20" Text="Bottom Dock Panel" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="0,0,10,0" /> </StackPanel>  
  29.             <StackPanel Controls:DockPanel.Dock="Right" Width="60" Background="LawnGreen" VerticalAlignment="Center" Orientation="Vertical" Height="397" Margin="0,-51,0,0">  
  30.                 <TextBlock TextAlignment="Center" Height="322" TextWrapping="Wrap" FontSize="20" VerticalAlignment="Bottom" Foreground="#FFD8471C" Margin="10,0,0,0">  
  31.                     <Run/>  
  32.                     <LineBreak/>  
  33.                     <Run/>  
  34.                     <LineBreak/>  
  35.                     <Run/>  
  36.                     <LineBreak/>  
  37.                     <Run/>  
  38.                     <LineBreak/>  
  39.                     <Run/>  
  40.                     <LineBreak/>  
  41.                     <Run/>  
  42.                     <LineBreak/>  
  43.                     <Run Text="Right Dock Panel" /> </TextBlock>  
  44.             </StackPanel>  
  45.         </Controls:DockPanel>  
  46.     </Grid>  
  47. </Page>  

Step 4

Deploy your app on Local Machine. The output of the UWPDockPanel is shown below.

 
Summary

Now, you have successfully tested DockPanel Control in XAML and UWP environment using Visual Studio 2017.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now