Universal Windows Platform – Docking 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 locate 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, then 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 to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)


Step 5

From the Menu choose Project, then Add New Item…


Step 6

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then set the Name as Docking.cs and then select Add to add the file to the Project


Step 7

Then in the Code View for Docking.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 Docking : 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 Docking 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 Docking panel)

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

        public static readonly DependencyProperty DockProperty = 
        DependencyProperty.RegisterAttached("Dock", typeof(Dock),
        typeof(Docking), 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);

Step 8

From the Menu choose Build and then Build Solution


Step 9

Then 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:

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

It should appear as such:


Step 12

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


Step 13

Once started the Application should then appear displaying the Docking Control with items docked to the Top, Bottom, Left and Right


Step 14

To Exit the application select Stop in Visual Studio


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 )

Google+ photo

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

Connecting to %s