Universal Windows Platform – Radial Control

Radial Control demonstrates how to create a Radial Panel 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.

vs2017

Step 2

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

vs2017-file-new-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 RadialControl and select a Location and then select Ok to create the Project
vs2017-new-project-radial-control

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

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…

vs2017-project-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 RadialPanel.cs before selecting Add to add the file to the Project

vs2017-add-new-item-radial-control

Step 7

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

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

namespace RadialControl
{
    public partial class RadialPanel : Panel
    {
        private bool _ignorePropertyChange;

        private static void OnIsOrientedPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            RadialPanel source = (RadialPanel)d;
            bool value = (bool)e.NewValue;
            if (source._ignorePropertyChange)
            {
                source._ignorePropertyChange = false;
                return;
            }
            source.InvalidateMeasure();
        }

        private static void OnItemHeightOrWidthPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            RadialPanel source = (RadialPanel)d;
            double value = (double)e.NewValue;
            if (source._ignorePropertyChange)
            {
                source._ignorePropertyChange = false;
                return;
            }
            if (!double.IsNaN(value) && ((value <= 0.0) || double.IsPositiveInfinity(value)))
            {
                source._ignorePropertyChange = true;
                source.SetValue(e.Property, (double)e.OldValue);
                throw new ArgumentException("OnItemHeightOrWidthPropertyChanged InvalidValue", "value");
            }
            source.InvalidateMeasure();
        }

        public static readonly DependencyProperty ItemHeightProperty =
        DependencyProperty.Register("ItemHeight", typeof(double), typeof(RadialPanel),
        new PropertyMetadata(double.NaN, OnItemHeightOrWidthPropertyChanged));

        public static readonly DependencyProperty ItemWidthProperty =
        DependencyProperty.Register("ItemWidth", typeof(double), typeof(RadialPanel),
        new PropertyMetadata(double.NaN, OnItemHeightOrWidthPropertyChanged));

        public static readonly DependencyProperty IsOrientedProperty =
        DependencyProperty.Register("IsOriented", typeof(bool), typeof(RadialPanel),
        new PropertyMetadata(false, OnIsOrientedPropertyChanged));

        public double ItemHeight
        {
            get { return (double)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        public double ItemWidth
        {
            get { return (double)GetValue(ItemWidthProperty); }
            set { SetValue(ItemWidthProperty, value); }
        }

        public bool IsOriented
        {
            get { return (bool)GetValue(IsOrientedProperty); }
            set { SetValue(IsOrientedProperty, value); }
        }

        protected override Size MeasureOverride(Size constraint)
        {
            double itemWidth = ItemWidth;
            double itemHeight = ItemHeight;
            bool hasFixedWidth = !double.IsNaN(itemWidth);
            bool hasFixedHeight = !double.IsNaN(itemHeight);
            Size itemSize = new Size(
                hasFixedWidth ? itemWidth : constraint.Width,
                hasFixedHeight ? itemHeight : constraint.Height);
            foreach (UIElement element in Children)
            {
                element.Measure(itemSize);
            }
            return itemSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double itemWidth = ItemWidth;
            double itemHeight = ItemHeight;
            bool hasFixedWidth = !double.IsNaN(itemWidth);
            bool hasFixedHeight = !double.IsNaN(itemHeight);
            double radiusX = finalSize.Width * 0.5;
            double radiusY = finalSize.Height * 0.5;
            double count = Children.Count;
            double deltaAngle = 2 * Math.PI / count;
            Point centre = new Point(finalSize.Width / 2,
                finalSize.Height / 2);
            for (int i = 0; i < count; i++)
            {
                UIElement element = Children[i];
                Size elementSize = new Size(
                hasFixedWidth ? itemWidth : element.DesiredSize.Width,
                hasFixedHeight ? itemHeight : element.DesiredSize.Height);
                double angle = i * deltaAngle;
                double x = centre.X + radiusX * Math.Cos(angle)
                    - elementSize.Width / 2;
                double y = centre.Y + radiusY * Math.Sin(angle)
                    - elementSize.Height / 2;
                if (IsOriented)
                {
                    element.RenderTransform = null;
                }
                else
                {
                    element.RenderTransformOrigin = new Point(0.5, 0.5);
                    element.RenderTransform = new RotateTransform()
                    {
                        Angle = angle * 180 / Math.PI
                    };
                }
                element.Arrange(new Rect(x, y,
                    elementSize.Width, elementSize.Height));
            }
            return finalSize;
        }
    }
}

In the Radial.cs there are using statements to include the necessary functionality. There is an OnIsOrientedPropertyChanged and OnItemHeightOrWidthPropertyChanged to respond to Events that will happen within the Control. There are DependencyProperty items and Properties for ItemHeight, ItemWidth and IsOriented. There is MeasureOverride and ArrangeOverride that will help to place Controls that will be Children of the Panel a and to position them at different degrees of rotation around a centre point.

Step 8

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

vs2017-build-build-solution

Step 9

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-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:RadialPanel Height="300" Width="300">
	<Rectangle Width="50" Height="50" Fill="Black"/>
	<Rectangle Width="50" Height="50" Fill="Gray"/>
	<Rectangle Width="50" Height="50" Fill="Red"/>
	<Rectangle Width="50" Height="50" Fill="Orange"/>
	<Rectangle Width="50" Height="50" Fill="Yellow"/>
	<Rectangle Width="50" Height="50" Fill="Green"/>
	<Rectangle Width="50" Height="50" Fill="Cyan"/>
	<Rectangle Width="50" Height="50" Fill="Blue"/>
	<Rectangle Width="50" Height="50" Fill="Magenta"/>
	<Rectangle Width="50" Height="50" Fill="Purple"/>
</local:RadialPanel>

The block of XAML represents the RadialPanel Control with its Children set to some 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

vs2017-local-machine

Step 13

After the Application started running it should then appear displaying the RadialPanel Control in the Application

ran-radial-control

Step 14

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

vs2017-close

Creative Commons License

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s