Working with ColorDialog in WPF

Working with ColorDialog in WPF

There are times when you need to ask your user to choose a color. In Windows there is a native ColorDialog window. This dialog is not very friendly to WPF applications however. This tip shows how to force the dialog to give you the color in the correct WPF format.

Pity the poor ColorDialog class. In Windows Forms it is a proud member of the dialog family. You can find it on the ToolBox, along with the other standard dialogs like FontDialog, OpenFileDialog etc. But when you transition to WPF you'll find that it is missing from the toolbox and that there is no WPF replacement available. But don't worry, in this tip I'll show you how to use the ColorDialog in your WPF application.

The ColorDialog in WinForms
As I mentioned earlier, you can find the ColorDialog in the Toolbox. Here's a screenshot.

Since ColorDialog is implemented as a Component, I can drag it to my Window Forms designer, and instantly have it added to the component tray.

Once it is in the component tray, I can select it and adjust its properties in the Properties Window.

Getting the color value in WinForms
Finally, to get the color I invoke the dialog like this.

How to use ColorDialog in WinForms
Before I can use the Windows Form dialog in my project I need to add a reference to the correct assembly. Since ColorDialog is a .NET class, it is easy to add a reference to its

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

assembly (System.Windows.Forms.dll) in a WPF project. I also need to add a reference to System.Drawing.dll to get access the System.Drawing.Color struct.

Now it is simple to invoke the dialog and convert the GDI colors to the WPF equivalent.

Other options
Since there is no native WPF color dialog, the WPF community has stepped up and createed custom versions. A simple Web search turns up a number of solutions. Here's one from CodeProject.


This was first published in August 2010

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.