Custom Color Picker for Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
Featured Article
16 Jun

This article explains how to create a custom "discrete" colour picker page for Windows Phone.

WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone SDK 8.0
Devices(s): Nokia Lumia 810
Platform(s): Windows Phone 8
Windows Phone 8
Created: Spaso Lazarevic (03 Jun 2013)
Last edited: hamishwillee (02 Jul 2013)



Working with Windows Phone applications sometimes you need to have a colour picker. Sometimes a picker like that provided in the Coding4Fun Toolkit, where you can pick from any colour in the spectrum, is appropriate. In other cases it makes more sense to offer a user a discrete selection of colours.

This article explains how to create a colour picker that offers a discrete set of colours, as shown below:

Colour picker page

First of all, we need to define XAML to get small colour rectangle in rows and columns. We will use regular ListBox with small addition of ItemPanelTemplate defined as WrapPanel. To use WrapPanel you need to use Windows Phone Toolkit or RadControls. In this example we use controls with WP toolkit.

Note.pngNote: The following code is tested on Windows Phone 8 but almost the same code would also work on Windows Phone 7.

XAML code for colour picker page

FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
<converters:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<RowDefinition Height="*"/>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Name="listBox" SelectionChanged="lstColor_SelectionChanged">
<StackPanel x:Name="item" Orientation="Horizontal" Margin="12, 6 0, 6">
<Rectangle Fill="{Binding Color, Converter={StaticResource ColorToBrushConverter}}"
Width="100" Height="100" />

To use toolkit you need to install it first because it is not part of the SDK for Windows Phone 8. The best way to install it is to use NuGet (tool that help you to install packages into your Visual Studio project) from Visual Studio: Tools/Library Package Manager/Package Manager Console, and the use next command: Install-Package WPtoolkit

NuGet Installed

If you don’t have NuGet I strongly suggest you to install it in you Visual Studio environment (Tools/Extensions and Updates NuGet Package Manager).

When you install WP toolkit, you need to include it in the XAML. I also add TiltEffect into the page to make it obvious what colour is selected (you'll see some small animation when you touch one of the rectangles filled with colour). Finally, in order to colour the rectangle, we used ColorToBrushConverter to fill the rectangle with the selected colour. Fill controls need SolidColorBrush type of object and we use converter to convert from Color type to SolidColorBrush type.

The size of the rectangle can change using Width and Height property of the rectangle. For small size we use 65 px and for large size we use 100 px. You can use size depending how many colours you want on your colour picker page.

When we have definition of our colour picker page, we need to fill these rectangle with colours. So, the next step is code behind explanation of our ColorPickerPage.

Code behind for color picker page

First we define colorNames and uintColors for definition of our custom colours:

static string[] colorNames =
static uint[] uintColors =

You can find list of colours in alphabetical order on Wiki page List of colors: A-M and List of colors: N-Z. For uintColors variable use Hex(RGB) value from Wiki page but add FF value after 0x so the format for uintColors is for example for yellow color: 0xFFFFFF00.

When ColorPickerPages is loaded we need to use all the colours from our static variable and use it as ItemSource for ListBox control:

private void ColorPickerPage_Loaded(object sender, RoutedEventArgs e)
List<ColorItem> item = new List<ColorItem>();
for (int i = 0; i < 66; i++)
item.Add(new ColorItem() { Text = colorNames[i], Color = ConvertColor(uintColors[i]) });
listBox.ItemsSource = item; //Fill ItemSource with all colors
private Color ConvertColor(uint uintCol)
byte A = (byte)((uintCol & 0xFF000000) >> 24);
byte R = (byte)((uintCol & 0x00FF0000) >> 16);
byte G = (byte)((uintCol & 0x0000FF00) >> 8);
byte B = (byte)((uintCol & 0x000000FF) >> 0);
return Color.FromArgb(A, R, G, B); ;

As our definition of our colours is in Hexadecimal format we use ConvertColor() function to split hex value into A,R,G,B component and use Color.FromArgb to get Color type that we could use in our app.

To store color item (text and color value) we use ColorItem class with two properties:

public class ColorItem
public string Text { get; set; }
public Color Color { get; set; }

And finally, when one of the color is selected SelectionChanged is triggered:

private void lstColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
if (e.AddedItems.Count > 0)
(Application.Current as App).CurrentColorItem = ((ColorItem)e.AddedItems[0]);

We use global CurrentColorItem property to store selected ColorItem. CurrentColorItem is defined in App.xaml.cs class as:

public ColorItem CurrentColorItem { set; get; }

How to call ColorPickerPage

When we have ColorPickerPage defined, we need to call it and receive colour and name of the colour. For that purpose we use MainPage.xaml. Let say, just for presentation purpose, that we want to fill a rectangle on the first page of our app with some custom colours and the name of the same colour in text box. The main purpose of main page is to show selected colour and name of the colour.

Custom Color Picker

Also, we use IsolatedStorageSettings to store last used ColorItem. When app is start for the first time, we choose some colour to be default (Cyan) and when we are navigating to the main page and if we have value for the global variable CurrentColorItem then means that we select some color from ColorPickerPage.


public partial class MainPage : PhoneApplicationPage
private ColorItem _colorItem;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
// Constructor
public MainPage()
private void InitializeSettings()
if (!settings.Contains("LastColorItem"))
ColorItem item = new ColorItem();
item.Color = Colors.Cyan;
item.Text = "Cyan";
settings.Add("LastColorItem", item);
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
settings["LastColorItem"] = _colorItem;
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
//get settings value
if (settings.Contains("LastColorItem"))
_colorItem = (ColorItem)settings["LastColorItem"];
ColorItem myColorItem = (Application.Current as App).CurrentColorItem;
if (myColorItem != null)
_colorItem = (ColorItem)myColorItem;
this.ColorName.Text = _colorItem.Text;
this.FillRect.Fill = new SolidColorBrush(_colorItem.Color);
private void PickColorButton_Click(object sender, RoutedEventArgs e)
NavigationService.Navigate(new Uri("/ColorPickerPage.xaml", UriKind.Relative));

In OnNavigatedTo method we use local variable _colorItem which is ColorItem type to fill Rectangle with colour and TextBox with text whether from last used color (LastColorItem stored in IsolatedStorageSettings) or from ColorPickerPage (using CurrentColorItem). When we are navigating from MainPage we store last used colour in IsolatedStorageSettings.


In MainPage we use three controls in StackPanel, Rectangle for colour, TextBox for name of the colour, Button for calling ColorPickerPage:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Vertical">
<Rectangle x:Name="FillRect" Width="400" Height="200" Stroke="White"
<TextBox x:Name="ColorName" HorizontalContentAlignment="Center" Width="400"/>
<Button Height="80" Width="200" Content="Pick Color" Click="PickColorButton_Click"/>

That is it!


This page was last modified on 2 July 2013, at 08:51.
405 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.