Introduction

In Visual Studio and other tools, we found that whenever we need to select a color for a particular element, a color combobox is being used. This article will guide you to make a combo box which binds all the system colors.

Background

The reader is assumed to be at least a beginner in WPF who knows the basics of data binding techniques, dependency properties, etc.

The extracted colors are binded to an ObjectDataProvider which serves as the datasource for the combobox. The combobox item template is divided into two sections, one for displaying the color and another for displaying name of color. Textblock is used for both and the name of color is binded to one of textblocks' text property and background of the others. The selectedvalue is binded to a dependency property "SelectedColor" of type Brush.

If you just want to return color , then just change all "Brush" in cs file to "Color" .
But if you want to bind a property in xaml for example "Background" you cannot use "Color" . you should use "brush"

First I tried to bind the SelectedColor via data binding to my viewmodel, but it did'nt work, so I tried
<local:Colorpicker SelectedColor="Aquamarine"/>
to see, if I can set an initial value, but it didn't work. So how can I set a SelectedColor ?

You would need to change the DP so that there was a property changed callback and then examine each of the ComboBox items and find the Brush that matched the SelectedColor DP and then set that to IsSelected.