Universal Windows Platform – Offset Control

Offset Control demonstrates how to create an Offset 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 OffsetControl and select a Location and then select Ok to create the Project
vs2017-new-project-offset-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 OffsetPanel.cs before selecting Add to add the file to the Project

vs2017-add-new-item-offset-control

Step 7

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

using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
 
namespace OffsetControl
{
    public class OffsetPanel : Panel
    {
        private bool ignorePropertyChange;
 
        private static void OnMaximumColumnsPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            OffsetPanel source = (OffsetPanel)d;
            int value = (int)e.NewValue;
            if (source.ignorePropertyChange)
            {
                source.ignorePropertyChange = false;
                return;
            }
            if (value < int.MinValue || value > int.MaxValue)
            {
                source.ignorePropertyChange = true;
                source.SetValue(e.Property, (int)e.OldValue);
                throw new ArgumentException("OnMaximumColumnsPropertyChanged InvalidValue", "value");
            }
            source.InvalidateMeasure();
        }
 
        private static void OnItemHeightOrWidthPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            OffsetPanel source = (OffsetPanel)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 MaximumColumnsProperty =
        DependencyProperty.Register("MaximumColumns", typeof(int), typeof(OffsetPanel),
        new PropertyMetadata(2, OnMaximumColumnsPropertyChanged));

        public static readonly DependencyProperty ColumnOffsetProperty =
        DependencyProperty.Register("ColumnOffset", typeof(double), typeof(OffsetPanel),
        new PropertyMetadata(10.0, OnItemHeightOrWidthPropertyChanged));

        public static readonly DependencyProperty RowOffsetProperty =
        DependencyProperty.Register("RowOffset", typeof(double), typeof(OffsetPanel),
        new PropertyMetadata(5.0, OnItemHeightOrWidthPropertyChanged));

        public static readonly DependencyProperty SpacingYProperty =
        DependencyProperty.Register("SpacingY", typeof(double), typeof(OffsetPanel),
        new PropertyMetadata(10.0, OnItemHeightOrWidthPropertyChanged));

        public static readonly DependencyProperty SpacingXProperty =
        DependencyProperty.Register("SpacingX", typeof(double), typeof(OffsetPanel),
        new PropertyMetadata(10.0, OnItemHeightOrWidthPropertyChanged));

        public int MaximumColumns
        {
            get { return (int)GetValue(MaximumColumnsProperty); }
            set { SetValue(MaximumColumnsProperty, value); }
        }
 
        public double ColumnOffset
        {
            get { return (double)GetValue(ColumnOffsetProperty); }
            set { SetValue(ColumnOffsetProperty, value); }
        }
  
        public double RowOffset
        {
            get { return (double)GetValue(RowOffsetProperty); }
            set { SetValue(RowOffsetProperty, value); }
        }
		
        public double SpacingX
        {
            get { return (double)GetValue(SpacingXProperty); }
            set { SetValue(SpacingXProperty, value); }
        }

        public double SpacingY
        {
            get { return (double)GetValue(SpacingYProperty); }
            set { SetValue(SpacingYProperty, value); }
        }
    
        protected override Size MeasureOverride(Size availableSize)
        {
            double itemWidth = 0.0;
            double itemHeight = 0.0;
            double x = 0;
            double y = 0;
            for (int i = 0; i < Children.Count; i++)
            {
                UIElement element = Children[i];
                element.Measure(availableSize);
                double width = element.DesiredSize.Width + x;
                double height = element.DesiredSize.Height + y;
                if (width > itemWidth) itemWidth = width;
                if (height > itemHeight) itemHeight = height;
                y += SpacingY;
                if ((i + 1) % MaximumColumns == 0)
                {
                    x -= SpacingX * (MaximumColumns - 1);
                    x += RowOffset;
                    y += ColumnOffset;
                }
                else
                {
                    x += SpacingX;
                }
            }
            return new Size(itemWidth, itemHeight);
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            double x = 0;
            double y = 0;
            for (int i = 0; i < Children.Count; i++)
            {
                UIElement element = Children[i];
                element.Arrange(new Rect(new Point(x, y),
                    element.DesiredSize));
                y += SpacingY;
                if ((i + 1) % MaximumColumns == 0)
                {
                    x -= SpacingX * (MaximumColumns - 1);
                    x += RowOffset;
                    y += ColumnOffset;
                }
                else
                {
                    x += SpacingX;
                }
            }
            return finalSize;
        }
    }
}

In the OffsetPanel.cs there are using statements to include the necessary functionality. There is an OnMaximumColumnsPropertyChanged and OnItemHeightOrWidthPropertyChanged to respond to Events that will happen within the Control. There are DependencyProperty items and Properties for MaximumColumns, ColumnOffset, RowOffset, SpacingX and SpacingY. There is MeasureOverride and ArrangeOverride that will help to place Controls that will be Children of the Panel and to position them at Offsets to each other.

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:OffsetPanel SpacingX="50" SpacingY="20" MaximumColumns="5"
	HorizontalAlignment="Center" VerticalAlignment="Center">
	<Rectangle Width="75" Height="75" Fill="Black"/>
	<Rectangle Width="75" Height="75" Fill="Gray"/>
	<Rectangle Width="75" Height="75" Fill="Red"/>
	<Rectangle Width="75" Height="75" Fill="Orange"/>
	<Rectangle Width="75" Height="75" Fill="Yellow"/>
	<Rectangle Width="75" Height="75" Fill="Green"/>
	<Rectangle Width="75" Height="75" Fill="Cyan"/>
	<Rectangle Width="75" Height="75" Fill="Blue"/>
	<Rectangle Width="75" Height="75" Fill="Magenta"/>
	<Rectangle Width="75" Height="75" Fill="Purple"/>
</local:OffsetPanel>

The block of XAML represents the OffsetPanel 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 OffsetPanel Control in the Application

ran-offset-control

Step 14

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

vs2017-close

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 )

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 )

Connecting to %s