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