Universal Windows Platform – Directs Control

Directs Control demonstrates how to create a Directional Pad control and also indicate which direction has been selected.

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 DirectsControl 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 Directs.cs before selecting Add to add the file to the Project


Step 7

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

using System;
using Windows.Devices.Input;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace DirectsControl
    public class Directs : Grid
        private const int size = 3;
        private const string path_up = "M 0,0 40,0 40,60 20,80 0,60 0,0 z";
        private const string path_down = "M 0,20 20,0 40,20 40,80 0,80 z";
        private const string path_left = "M 0,0 60,0 80,20 60,40 0,40 z";
        private const string path_right = "M 0,20 20,0 80,0 80,40 20,40 z";

        public enum Directions
            Up = 0,
            Down = 1,
            Left = 2,
            Right = 3

        public delegate void DirectionEvent(object sender, Directions direction);
        public event DirectionEvent Direction;

        public static readonly DependencyProperty ForegroundProperty =
        DependencyProperty.Register("Foreground", typeof(Brush),
        typeof(Directs), null);

        public Brush Foreground
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }

        private Path StringToPath(string value)
            return (Path)XamlReader.Load(
                $"<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Path.Data>{value}</Path.Data></Path>"

        private void Path_PointerMoved(object sender, PointerRoutedEventArgs e)
            PointerPoint point = e.GetCurrentPoint(this);
            bool fire = (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse) ?
                point.Properties.IsLeftButtonPressed : point.IsInContact;
            if (fire)
                Path path = ((Path)sender);
                if (Direction != null)
                    this.Direction(path, (Directions)Enum.Parse(typeof(Directions), path.Name));

        private void Add(ref Grid grid,
            string name, string value, 
            int row, int column, 
            int? rowspan, int? columnspan,
            VerticalAlignment? vertical = null, 
            HorizontalAlignment? horizontal = null)
            Path path = StringToPath(value);
            path.Name = name;
            path.Margin = new Thickness(5);
            if (vertical != null) path.VerticalAlignment = vertical.Value;
            if (horizontal != null) path.HorizontalAlignment = horizontal.Value;
            path.SetBinding(Path.FillProperty, new Binding()
                Source = this,
                Path = new PropertyPath("Foreground"),
                Mode = BindingMode.TwoWay
            path.PointerMoved += Path_PointerMoved;
            path.SetValue(Grid.RowProperty, row);
            path.SetValue(Grid.ColumnProperty, column);
            if (rowspan != null) path.SetValue(Grid.RowSpanProperty, rowspan);
            if (columnspan != null) path.SetValue(Grid.ColumnSpanProperty, columnspan);

        public Directs()
            Grid grid = new Grid()
                Height = 180,
                Width = 180
            for (int index = 0; (index < size); index++)
                grid.RowDefinitions.Add(new RowDefinition()
                    Height = (index == 1) ? GridLength.Auto : new GridLength(100, GridUnitType.Star)
                grid.ColumnDefinitions.Add(new ColumnDefinition()
                    Width = (index == 1) ? GridLength.Auto : new GridLength(100, GridUnitType.Star)
            Add(ref grid, "Up", path_up, 0, 1, 2, null, VerticalAlignment.Top, null);
            Add(ref grid, "Down", path_down, 1, 1, 2, null, VerticalAlignment.Bottom, null);
            Add(ref grid, "Left", path_left, 1, 0, null, 2, null, HorizontalAlignment.Left);
            Add(ref grid, "Right", path_right, 1, 1, null, 2, null, HorizontalAlignment.Right);
            Viewbox box = new Viewbox()
                Child = grid

There are const Values for the string items for the shapes that will make up each part of the Directional Pad, then there’s an enum to represent the Directions, a DirectionEvent for when the Direction changes and a DependencyProperty for the Foreground of the Control. There is a Method to convert the shapes into a Path to be displayed and a Path_PointerMoved Method that will respond to Events raised by each part of the Directional Pad, the Add Method is used to create each part of the Directional Pad and is used by the Directs Constructor to create the look-and-feel of the Control create the Up, Down, Left and Right parts.

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:

<StackPanel Margin="50" VerticalAlignment="Center" HorizontalAlignment="Center">
	<TextBlock Name="Label" HorizontalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/>
	<local:Directs x:Name="Pad" Height="400" Width="400" Foreground="{ThemeResource AccentButtonBackground}" Direction="Pad_Direction"/>

The MainPage has a StackPanel with a TextBlock to indicate the Direction selected and the Directs Control itself with the Direction Event Handler set.

Step 12

From the Menu choose View and then Code


Step 13

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

private void Pad_Direction(object sender, Directs.Directions direction)
	Label.Text = direction.ToString();

Below the MainPage() Method there is the Pad_Direction Event Handler which will set the TextBlock to be the Direction selected by the Directs Control

Step 14

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


Step 15

After the Application has started running you can then select the parts of the Directs Control for Up, Down, Left and Right.


Step 16

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


This control shows how to create a control that emulates the behaviour of a physical Directional Pad – that is when you press a Direction it continues to register that Direction plus when it’s changed to another one, this can be with a continuous press of a Mouse, Pen or Touch to switch between the different directions. Although this example is simple and just displays display text in a Label to show the Direction, it could form the basis of a game controller.

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 )

Facebook photo

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

Connecting to %s