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 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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

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.