Universal Windows Platform – Colour Control

Colour Control demonstrates how to create a ColourSelector 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 ColourControl and select a Location and then select Ok to create the Project
vs2017-new-project-colour-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 ColourSelector.cs before selecting Add to add the file to the Project

vs2017-add-new-item-colour-control

Step 7

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

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace ColourControl
{
    public class ColourSelector : ComboBox
    {
        public class Colour
        {
            public string Name { get; set; }
            public Color Value { get; set; }
        }

        private static IEnumerable<string> SplitCapital(string text)
        {
            Regex regex = new Regex(@"\p{Lu}\p{Ll}*");
            foreach (Match match in regex.Matches(text))
            {
                yield return match.Value;
            }
        }

        private List<Colour> _colours = typeof(Colors)
        .GetRuntimeProperties()
        .Select(c => new Colour
        {
            Value = (Color)c.GetValue(null),
            Name = string.Join(" ", SplitCapital(c.Name))
        }).ToList();

        public ColourSelector()
        {
            ItemTemplate = (DataTemplate)XamlReader.Load(
            "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
            "<StackPanel Orientation=\"Horizontal\">" +
            "<Rectangle Width=\"20\" Height=\"15\" Margin=\"5,0\"><Rectangle.Fill>" +
            "<SolidColorBrush Color=\"{Binding Value}\"/></Rectangle.Fill></Rectangle>" +
            "<TextBlock VerticalAlignment=\"Center\" Text=\"{Binding Name}\"/>" +
            "</StackPanel></DataTemplate>");
            SelectedValuePath = "Value";
            ItemsSource = _colours;
        }

        public Color Selected
        {
            get { return ((Colour)SelectedItem).Value; }
            set { SelectedItem = (_colours.Single(w => w.Value == value)); }
        }
    }
}

In the ColourSelector.cs there are using statements to include the necessary functionality. There is a Colour Class to represent a single item and a SplitCapital Method which is used to normalise the name of a colour so it appears split by capital letters, such as CornflowerBlue appearing as Cornflower Blue. There is a Member for all the Colours which is a List of Colour Items and uses the SplitCapital to create the labels used for a Colour. The ColourSelector Constructor is used to set the ItemTemplate of the Control which is based upon a ComboBox and to set the ItemsSource to the set of Colours and there is a Selected Property to get or set the Colour.

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:

<Grid Margin="50">
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<local:ColourSelector Grid.Row="0" x:Name="Picker" HorizontalAlignment="Stretch"
	SelectionChanged="Selector_SelectionChanged"/>
	<Rectangle Grid.Row="1" Name="Display" Loaded="Display_Loaded"/>
</Grid>

The block of XAML contains a Grid with two rows, the first is for the ColourSelector itself and the SelectionChanged Event has been bound to an Event Handler and the second row is for a Rectangle and has it’s Loaded Event bound to an Event Handler.

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

private void Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	Display.Fill = new SolidColorBrush(Picker.Selected);
}

private void Display_Loaded(object sender, RoutedEventArgs e)
{
	Picker.Selected = Windows.UI.Colors.WhiteSmoke;
}

Below the MainPage() Method there is a Selector_SelectionChanged Method which will handle when the ColourSelector has its Selected Colour changed – it will set the Fill of the Rectangle to the Selected Value and Display_Loaded which sets the intial Selected Value of the ColourSelector.

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

vs2017-local-machine

Step 15

After the Application has started running you can select a Colour from the list to see the Fill of the Rectangle set to it in the Application

ran-colour-control

Step 16

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 )

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