Universal Windows Platform – Uniform 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.

vs2017-home

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 UniformControl, then select a Location and then select Ok to create the Project

vs2017-new-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)

vs2017-platform

Step 5

From the Menu choose Project, then Add New Item…

vs2017-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 Uniform.cs and then select Add to add the file to the Project

class-uniform-control

Step 7

Then in the Code View for Uniform.cs the following should be entered:

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

namespace UniformControl
{
    public class Uniform : Panel
    {
        private int _columns;
        private int _rows;

        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(int),
            typeof(Uniform), new PropertyMetadata(0));

        public static readonly DependencyProperty FirstColumnProperty =
        DependencyProperty.Register("FirstColumn", typeof(int),
            typeof(Uniform), new PropertyMetadata(0));

        public static readonly DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int),
                typeof(Uniform), new PropertyMetadata(0));

        public int Columns
        {
            get { return (int)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        public int FirstColumn
        {
            get { return (int)GetValue(FirstColumnProperty); }
            set { SetValue(FirstColumnProperty, value); }
        }

        public int Rows
        {
            get { return (int)GetValue(RowsProperty); }
            set { SetValue(RowsProperty, value); }
        }

        private void UpdateComputedValues()
        {
            _columns = Columns;
            _rows = Rows;
            if (FirstColumn >= _columns) FirstColumn = 0;
            if ((_rows == 0) || (_columns == 0))
            {
                var row = 0;
                var column = 0;
                int count = Children.Count;
                while (column < count)
                {
                    UIElement element = Children[column];
                    if (element.Visibility != Visibility.Collapsed)
                    {
                        row++;
                    }
                    column++;
                }
                if (row == 0) row = 1;
                if (_rows == 0)
                {
                    if (_columns > 0)
                    {
                        _rows = ((row + FirstColumn) + (_columns - 1)) / _columns;
                    }
                    else
                    {
                        _rows = (int)Math.Sqrt(row);
                        if ((_rows * _rows) < row)
                        {
                            _rows++;
                        }
                        _columns = _rows;
                    }
                }
                else if (_columns == 0)
                {
                    _columns = (row + (_rows - 1)) / _rows;
                }
            }
        }

        protected override Size ArrangeOverride(Size size)
        {
            Rect rectangle = new Rect(0.0, 0.0,
                size.Width / _columns, size.Height / _rows);
            double width = rectangle.Width;
            double value = size.Width - 1.0;
            rectangle.X += rectangle.Width * FirstColumn;
            foreach (UIElement element in Children)
            {
                element.Arrange(rectangle);
                if (element.Visibility != Visibility.Collapsed)
                {
                    rectangle.X += width;
                    if (rectangle.X >= value)
                    {
                        rectangle.Y += rectangle.Height;
                        rectangle.X = 0.0;
                    }
                }
            }
            return size;
        }

        protected override Size MeasureOverride(Size size)
        {
            UpdateComputedValues();
            Size available = new Size(size.Width / (_columns), size.Height / (_rows));
            double width = 0.0;
            double height = 0.0;
            int value = 0;
            int count = Children.Count;
            while (value < count)
            {
                UIElement element = Children[value];
                element.Measure(available);
                Size desired = element.DesiredSize;
                if (width < desired.Width) width = desired.Width;
                if (height < desired.Height) height = desired.Height;
                value++;
            }
            return new Size(width * _columns, height * _rows);
        }
    }
}

Step 8

From the Menu choose Build and then Build Solution

vs2017-build

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-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:Uniform HorizontalAlignment="Center" VerticalAlignment="Center">
	<Rectangle Width="75" Height="75" Fill="Black" Margin="5"/>
	<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:Uniform>

It should appear as such:

xaml-uniform-control

Step 12

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

vs2017-debug

Step 13

Once started the Application should then appear displaying the Uniform Control with some items where the size of each element is determined by the first item in the control

run-uniform-control

Step 14

To Exit the application select Stop in Visual Studio

vs2017-stop

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 )

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