Click here to Skip to main content
15,896,730 members
Articles / Desktop Programming / WPF

WPF: A Simple Color Picker With Preview

Rate me:
Please Sign up or sign in to vote.
4.89/5 (73 votes)
17 Apr 2012CPOL4 min read 295.4K   7.4K   135  
A simple Color Picker with preview.
using System;
using System.Windows;
using System.Windows.Media;

namespace WPFColorPickerLib
{
  /// <summary>
  /// Holds a ColorPicker control, and exposes the ColorPicker SelectedColor.
  /// 
  /// Enhanced by Mark Treadwell (1/2/10) to include:
  ///  - Added ability to set ColorPicker initial color via constructor or property
  ///  - Use of Button's IsDefault and IsCancel properties
  ///  - Setting tab behavior
  /// </summary>
  public partial class ColorDialog : Window
  {
    #region Constructors

    /// <summary>
    /// Default constructor initializes to Black.
    /// </summary>
    public ColorDialog()
      : this(Colors.Black)
    { }

    /// <summary>
    /// Constructor with an initial color.
    /// </summary>
    /// <param name="initialColor">Color to set the ColorPicker to.</param>
    public ColorDialog(Color initialColor)
    {
      InitializeComponent();
      colorPicker.InitialColor = initialColor;
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets/sets the ColorDialog color.
    /// </summary>
    public Color SelectedColor
    {
      get { return colorPicker.SelectedColor; }
      set { colorPicker.InitialColor = value; }
    }

    #endregion

    #region Event Handlers

    /// <summary>
    /// Close ColorDialog, accepting color selection.
    /// </summary>
    private void btnOK_Click(object sender, RoutedEventArgs e)
    {
      DialogResult = true;
    }

    /// <summary>
    ///  Close ColorDialog, rejecting color selection.
    /// </summary>
    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
      DialogResult = false;
    }

    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions