Coding4Fun WP7 ColorPicker in depth
published on: 5/9/2011 | Views: N/A | Tags: C4FToolkit
by WindowsPhoneGeek
In this article I am going to talk about the new ColorPicker control from the FREE Coding4fun Toolkit in details. I will explain everything about the main features, available public API, and will give lots of examples in different scenarios.
Basically ColorPicker is an UI component that displays a range of colors in a color space. It consists of a ColorSlider element as well as multiple Rectangle/Ellipse elements. As its name says it is a kind of extended picker that shows the selected color via its Color property.

Getting Started
To begin using ColorPicker first add a reference to the Coding4Fun.Phone.Controls.dll assembly.
NOTE: You can download this assembly from here: Coding4Fun Toolkit .
The next step is to add the "c4fToolkit" prefix declaration. Make sure that your page declaration includes the "c4fToolkit" namespace:
xmlns:c4fToolkit="clr-namespace:Coding4Fun.Phone.Controls;assembly=Coding4Fun.Phone.Controls"
The sample code should looks like:
<c4fToolkit:ColorPicker Height="300" Width="400"/>
Key Properties
ColorPicker exposes the following public properties:
- Color
This is a dependency property of type Color. It determine the current selected Color of the ColorPicker control.
- SolidColorBrush
This is a dependency property of type SolidColorBrush. It determine the current SolidColorBrush color of the ColorPicker control.
Events
- ColorChanged
This event occurs when the selected color has changed.
Examples
1. ColorPicker Sample Usage
In this example I will demonstrate how to use the ColorPicker in the easiest way.
<c4fToolkit:ColorPicker Height="300" Width="400"/>
2. ColorPicker SolidColorBrush Binding
In this example I will demonstrate how to Bind ColorPicker selected Color to a Rectangle Fill property using ElementBinding.
NOTE: For more information about ElementBinding take a look at our previous post: WP7 Element Binding samples
<c4fToolkit:ColorPicker x:Name="picker" Height="300" Width="400"/>
<TextBlock Text="ColorPicker Color Binding:"/>
<Rectangle Height="50" Width="50" Fill="{Binding ElementName=picker, Path=SolidColorBrush}" />
3. ColorPicker ColorChanged
In this example I will demonstrate how to display ColorPicker selected Color in a Rectangle using ColorChanged event .
<c4fToolkit:ColorPicker x:Name="picker" Height="300" Width="400" ColorChanged="picker_ColorChanged" /> <TextBlock Text="ColorPicker Color Binding:"/> <Rectangle Height="50" Width="50" x:Name="ColorRect"/>
private void picker_ColorChanged(object sender, Color color)
{
this.ColorRect.Fill = new SolidColorBrush(color);
}
4. Creating ColorPicker programmatically
In this example I will demonstrate how to create a ColorPicker programmatically:
private void Button_Click(object sender, RoutedEventArgs e)
{
ColorPicker colorPicler = new ColorPicker();
colorPicler.Height = 200;
colorPicler.Width = 200;
this.ContentPanel.Children.Add(colorPicler);
}
5. Customizing ColorPicker Style
In this example I will demonstrate how to customize the Style of the ColorPicker. At first we will add an image (pointer) inside the SampleSelector panel(this is an element from the below ControlTemplate). Next we will place the Rectangle bound to the SolidColorBrush at the bottom. In a similar way just by modifying the default Style you can customize the ColrPicker control in various ways.
<phone:PhoneApplicationPage.Resources>
<Style TargetType="c4fToolkit:ColorPicker" x:Key="customStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c4fToolkit:ColorPicker">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<c4fToolkit:ColorSlider
Width="24"
Margin="0,0,12,0"
IsColorVisible="false"
x:Name="ColorSlider"
Grid.Row="1"/>
<Grid
Name="Body"
Grid.Column="1"
Grid.Row="1">
<Rectangle
Name="SelectedHueColor"
Fill="Red" />
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,1" EndPoint="0, 0">
<GradientStop Offset="0" Color="Black"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid
Name="SampleSelector"
VerticalAlignment="Top"
HorizontalAlignment="Left">
<Image Source="pointer.png" Height="50" Width="50"/>
</Grid>
</Grid>
<Rectangle
Margin="0,10,0,12"
Height="24"
Grid.Row="2"
Grid.ColumnSpan="2"
Fill="{TemplateBinding SolidColorBrush}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
<c4fToolkit:ColorPicker Style="{StaticResource customStyle}" Height="300" Width="300" />
Here is how the final result should look like:
That was all about the ColorPicker control from the Coding4fun Toolkit in depth. You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
How to set the color through code
posted by: Punit Ganshani on 5/12/2011 12:05:50 AM
How to set the color through code?
The changed color is not reflecting on the screen, and it always displays Green color.
RE:How to set the color through code
posted by: windowsphonegeek on 5/15/2011 3:52:53 PM
This issue was fixed in the latest build of the Coding4Fun toolkit change set -> 66623 , work item: 6619 So, you can now download the updated assemblies with the fix: http://coding4fun.codeplex.com/SourceControl/list/changesets
How to use custom style with default pointer
posted by: LukeP on 1/4/2012 12:48:33 AM
Great article!
How can I customise the control but still use the default pointer?
Thanks
Color Picker is not working
posted by: Peacemaker on 1/30/2012 1:48:35 PM
I am getting this error...when I am trying to load the XAML having color picker in it..I am getting the following error..
XamlParseException Unknown parser error: Scanner 2147500037. [Line: 34 Position: 36].. Thanks in advance :)
Color Picker not working
posted by: sahreeg on 4/27/2012 10:55:03 AM
I get this error in my xml :
Error 2 The type 'c4fToolkit:ColorPicker' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have
when I am initializing the ColorPicker through the code you provided. Keep in mind I am sure I included the assembler and the namespace correctly
thanks
Error
posted by: Batuzay on 5/3/2012 9:37:07 PM
Im getting the same error as @sahreeg and i also have all the assemblies. right. but i can not use this code. i havent been able to make it work. even copy and paste wouldn't make it work.!!
Corrected errors
posted by: Batuzay on 5/3/2012 9:58:37 PM
When the Dll's are downloaded from the link above. they are Flag as "BLOCKED" by MSFT. just go to the source folder where the Dll's are held, and then make right click in them --> Properties --> and then click unblock button. its at the bottom of the page. and that will let VS work with the assembly.
How to get selected color to next page
posted by: Jasper Manickaraj on 5/16/2012 2:42:00 PM
I follow the Colorpicker control step by steps and it works well. But How i can get the selected color and pass the color to the next page and apply the selected color to a particular control like Line.stroke, Rectangle.fill.. here how i can assign the color value. Please anyone help me to get out of this one..
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
