SIGN UP MEMBER LOGIN:    
ARTICLE

WPF ColorCanvas

Posted by Mahesh Chand Articles | WPF with C# September 25, 2011
The ColorCanvas allows you to select a color either using an advanced color canvas, by setting the hexa-decimal values, or by setting the ARGB values. The ColorCanvas control comes with the WPF Toolkit Extended. This article demonstrates how to use the ColorCanvas control in a WPF application using C# and XAML.
Reader Level:

WPF ColorCanvas

The ColorCanvas allows you to select a color either using an advanced color canvas, by setting the hexa-decimal values, or by setting the ARGB values. The ColorCanvas control comes with the WPF Toolkit Extended. This article demonstrates how to use the ColorCanvas control in a WPF application using C# and XAML.  

Adding Reference to WPF Toolkit Extended Assembly

The ColorCanvas control is a part of the WPF Toolkit Extended and does not come with Visual Studio 2010. To use the ColorCanvas control in your application, you must add reference to the WPFToolkit.Extended.dll assembly. You can download Extended WPF Tookit from the CodePlex or you can use the WPFToolkit.Extended.dll available with this download. All you need is the DLL. See Downloads section of this article. You can find more details in my blog Adding Reference to WPF Toolkit Extended.

 

Creating a ColorCanvas

The default ColorCanvas looks like Figure 1. Listing 1 creates a simple ColorCanvas control.  

<wpfx:ColorCanvas Name="ColorCanvas1" />

 

Listing 1

ColorCanvas1.jpg

Figure 1

Properties

The SelectedColor property represents the currently selected color.

The A, R, G, and B properties represent the alpha, red, green, and blue values of a color respectively.

The HexaDecimalString property represents the hexadecimal value of the selected color.

The SelectedColorChanged event is fired when the color is changed in the ColorCanvas. Listing 2 implements the event and sets the background color of a TextBlock control to the selected color of the ColorCanvas.

<wpfx:ColorCanvas Name="ColorCanvas1" Margin="0,0,190,50"

                    SelectedColorChanged="ColorCanvas1_SelectedColorChanged"/>

 

private void ColorCanvas1_SelectedColorChanged(object sender,

    RoutedPropertyChangedEventArgs<Color> e)

{

    textBlock1.Background = new SolidColorBrush(ColorCanvas1.SelectedColor);

}

 

Listing 2

 ColorCanvas2.jpg

Figure 2

Summary

In this article, I discussed how we can use a ColorCanvas control in a WPF application using C# and XAML.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor