WPF Color Picker Implementation

The easiest way is to use ColorDialog which were used in WinForms.

 System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog();
 if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 {
      DrawBallColorBtn.Background = new SolidColorBrush(Color.FromArgb(colorDialog.Color.A, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B));
      _drawBallColor = colorDialog.Color.Name.ToLower();
 }

If you are targeting to a .NetCore 3.x add the fallowing to the .csproj file

<PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms> 
</PropertyGroup>

You can check the Color Picker Control of WpfToolKit Extended. This toolkit has many useful controls.


As Jodha said, you should use the Color Picker Control from the WpfToolkit Extended. Implementing the Color Picker Control is easy, simply do something like this:

Put this in your Window object:

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

And this wherever you want your color picker.

<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged"></xctk:ColorPicker>

Then, all you have to do is use the SelectedColorChanged event to change the text in the textbox, like this:

private void ClrPcker_Background_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
    TextBox.Text = "#" + ClrPcker_Background.SelectedColor.R.ToString() + ClrPcker_Background.SelectedColor.G.ToString() + ClrPcker_Background.SelectedColor.B.ToString();
}

Hope this helps!


Have a look at ColorBox control at codeplex. You can also create Linear and Radial gradients with it.

Tags:

C#

Wpf