Universal Windows Platform – Docking Control

Docking Control demonstrates how to create a DockingPanel which is a Panel where the elements can be docked to the Top, Bottom, Left or Right of the Control.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in the Name as DockingControl and select a Location and then select Ok to create the Project

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.


The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…


Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as DockingPanel.cs before selecting Add to add the file to the Project


Step 7

Once in the Code View for DockingPanel.cs the following should be entered:

using System;
using System.Globalization;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace DockingControl
    public class DockingPanel : Panel
        private static bool _ignorePropertyChange;

        public enum Dock
            Left = 0,
            Top = 1,
            Right = 2,
            Bottom = 3

        private static void OnLastChildFillPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
            if (d is DockingPanel panel)

        private static void OnDockPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            if (_ignorePropertyChange)
                _ignorePropertyChange = false;
            UIElement element = (UIElement)d;
            Dock value = (Dock)e.NewValue;
            if ((value != Dock.Left) && (value != Dock.Top) &&
                (value != Dock.Right) && (value != Dock.Bottom))
                _ignorePropertyChange = true;
                element.SetValue(DockProperty, (Dock)e.OldValue);
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "InvalidValue", value), "value");
            if (VisualTreeHelper.GetParent(element) is DockingPanel panel)

        public static readonly DependencyProperty LastChildFillProperty =
        DependencyProperty.Register("LastChildFill", typeof(bool),
        typeof(DockingPanel), new PropertyMetadata(true, OnLastChildFillPropertyChanged));

        public static readonly DependencyProperty DockProperty =
        DependencyProperty.RegisterAttached("Dock", typeof(Dock),
        typeof(DockingPanel), new PropertyMetadata(Dock.Left, OnDockPropertyChanged));

        public bool LastChildFill
            get { return (bool)GetValue(LastChildFillProperty); }
            set { SetValue(LastChildFillProperty, value); }

        public static Dock GetDock(UIElement element)
            if (element == null) throw new ArgumentNullException("element");
            return (Dock)element.GetValue(DockProperty);

        public static void SetDock(UIElement element, Dock dock)
            if (element == null) throw new ArgumentNullException("element");
            element.SetValue(DockProperty, dock);

        protected override Size ArrangeOverride(Size size)
            double left = 0.0;
            double top = 0.0;
            double right = 0.0;
            double bottom = 0.0;
            UIElementCollection children = Children;
            int count = children.Count - (LastChildFill ? 1 : 0);
            int index = 0;
            foreach (UIElement element in children)
                Rect rect = new Rect(left, top,
                  Math.Max(0.0, size.Width - left - right),
                  Math.Max(0.0, size.Height - top - bottom));
                if (index < count)
                    Size desiredSize = element.DesiredSize;
                    switch (GetDock(element))
                        case Dock.Left:
                            left += desiredSize.Width;
                            rect.Width = desiredSize.Width;
                        case Dock.Top:
                            top += desiredSize.Height;
                            rect.Height = desiredSize.Height;
                        case Dock.Right:
                            right += desiredSize.Width;
                            rect.X = Math.Max(0.0, size.Width - right);
                            rect.Width = desiredSize.Width;
                        case Dock.Bottom:
                            bottom += desiredSize.Height;
                            rect.Y = Math.Max(0.0, size.Height - bottom);
                            rect.Height = desiredSize.Height;
            return size;

        protected override Size MeasureOverride(Size size)
            double width = 0.0;
            double height = 0.0;
            double maxWidth = 0.0;
            double maxHeight = 0.0;
            foreach (UIElement element in Children)
                Size remainingSize = new Size(
                    Math.Max(0.0, size.Width - width),
                    Math.Max(0.0, size.Height - height));
                Size desired = element.DesiredSize;
                switch (GetDock(element))
                    case Dock.Left:
                    case Dock.Right:
                        maxHeight = Math.Max(maxHeight, height + desired.Height);
                        width += desired.Width;
                    case Dock.Top:
                    case Dock.Bottom:
                        maxWidth = Math.Max(maxWidth, width + desired.Width);
                        height += desired.Height;
            maxWidth = Math.Max(maxWidth, width);
            maxHeight = Math.Max(maxHeight, height);
            return new Size(maxWidth, maxHeight);

The DockingPanel Class Inherits the Panel Class and then has a bool value for ignoring property changes then defines an enum for the “Dock” positions of Left, Top, Right and Bottom. There’s an OnLastChildFillPropertyChanged event handler to call InvalidateArrange and OnDockPropertyChanged event handler which checkes the “_ignorePropertyChange” value and returns if true or gets the UIElement and checks the NewValue and adjusts the DockProperty accordingly based on it being none of the Dock values and then calls the InvalidateMeasure if needed if it’s a nested Docking Control.

There’s a pair of DependencyProperty for LastChildFill and Dock which set the relevent bool and Dock values. ArrangeOverride responds to Size changes that require the layout to be updated and goes through all the UIElement Children and creates a Rect to then get the correct sizes based on the Dock value to set the correct Width or Height. MeasureOverride returns a Size based on working out for each UIElement of the control and getting the correct sizes based on the Dock values and works out the Size to return based from the calculated “maxWidth” and “maxHeight”.

Step 8

Once done select from the Menu, Build, then Build Solution


Step 9

In the Solution Explorer select MainPage.xaml


Step 10

From the Menu choose View and then Designer


Step 11

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Viewbox Margin="50">
	<local:DockingPanel HorizontalAlignment="Center" VerticalAlignment="Center">
		<Rectangle Width="100" Height="100" Fill="Red" Margin="10" local:DockingPanel.Dock="Top"/>
		<Rectangle Width="100" Height="100" Fill="Orange" Margin="10" local:DockingPanel.Dock="Bottom"/>
		<Rectangle Width="100" Height="100" Fill="Yellow" Margin="10" local:DockingPanel.Dock="Left"/>
		<Rectangle Width="100" Height="100" Fill="Green" Margin="10" local:DockingPanel.Dock="Right"/>
		<Rectangle Width="100" Height="100" Fill="Cyan" Margin="10"/>

The MainPage has a Viewbox with the DockingPanel Control itself with the Children set to a set of Rectangle Controls.

Step 12

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 13

After the Application has started it should then appear displaying the UniformPanel Control with some items where the size of each element is determined by the first item in the control


Step 14

To Exit the Application select the Close button in the top right of the Application


Docking Control is based off the Dock Panel in the Silverlight Toolkit by Microsoft although with the Metro, Modern and Fluent design language progression this control doesn’t fit with the current guidelines for interfaces in general based on the Universal Windows Platform but it may still be useful for certain pages within an application where being able to dock elements to the Top, Bottom, Left or Right.

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s