Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF Color Picker Construction Kit

, 26 Dec 2010 CPOL
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
ColorPickerDemo.zip
ColorPicker.dll
ColorPickerControls.dll
ColorPickerDemo.exe
ColorPickerSource.zip
ColorPickerDemo
ColorPicker
ColorModels
CMY
CMYK
HSB
Lab
RGB
ExtensionMethods
Properties
Settings.settings
Resources
ColorPickerControls
Chips
Dialogs
Pickers
Properties
Settings.settings
ColorPickerDemo
ColorPickerDemo.ico
Properties
Settings.settings
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using ColorPickerControls;
using ColorPickerControls.Dialogs;

namespace ColorPickerDemo
{
    /// <summary>
    /// Interaction logic for MainWindow2.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }

       

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
    
        }

        private void colorRect_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var dia = new ColorPickerFullDialog();
            dia.InitialColor =((SolidColorBrush ) colorRect.Fill).Color ; //set the initial color
            if (dia.ShowDialog() == true)
            {
                colorRect.Fill = new SolidColorBrush(dia.SelectedColor); //do something with the selected color
            }
        }

        private void alphaColorRect_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var dia = new ColorPickerFullWithAlphaDialog();
            dia.InitialColor = ((SolidColorBrush)alphaColorRect.Fill).Color; //set the initial color
            if (dia.ShowDialog() == true)
            {
                alphaColorRect.Fill = new SolidColorBrush(dia.SelectedColor); //do something with the selected color
            }

        }


        #region ForegroundBackGroundColorChip via events

        //One can Bind to the ForegroundBrushChanged or BackgroundBrushChanged events
        private void foregroundBackgroundChip_ForegroundBrushChanged(object sender, ColorPicker.EventArgs<SolidColorBrush> e)
        {
            if (rectFGEvent != null)
            {
                rectFGEvent.Stroke  = e.Value;
            }
        }

        //One can Bind to the ForegroundColorChanged or BackgroundColorChanged events
        private void foregroundBackgroundChip_BackgroundColorChanged(object sender, ColorPicker.EventArgs<Color> e)
        {
            if (rectFGEvent != null)
            {
                rectFGEvent.Fill =  new SolidColorBrush(e.Value);
            }

        }

       #endregion

        #region Standard
       

        private void exStandard_Expanded(object sender, RoutedEventArgs e)
        {
            cpStandard.InitialColor = cdStandard.Color;
            cpStandard.SelectedColor = cdStandard.Color;
        }

        private void btStandardAccept_Click(object sender, RoutedEventArgs e)
        {
            cdStandard.Color = cpStandard.SelectedColor;
            exStandard.IsExpanded = false;
        }

        private void btStandardCancel_Click(object sender, RoutedEventArgs e)
        {
            exStandard.IsExpanded = false;
        }

       #endregion

        #region StandardAlpha
       

        private void btStandardAlphaAccept_Click(object sender, RoutedEventArgs e)
        {
           cdStandardAlpha.Color  = cpStandardWithAlpha.SelectedColor;
            exStandardAlpha.IsExpanded = false;
        }

        private void btStandardAlphaCancel_Click(object sender, RoutedEventArgs e)
        {
            exStandardAlpha.IsExpanded = false;
        }

        private void exStandardAlpha_Expanded(object sender, RoutedEventArgs e)
        {
            cpStandardWithAlpha.InitialColor = cdStandardAlpha.Color;
            cpStandardWithAlpha.SelectedColor = cdStandardAlpha.Color;
        }
     #endregion

        #region Full
       

        private void exFull_Expanded(object sender, RoutedEventArgs e)
        {
            cpFull.InitialColor = cdFull.Color;
            cpFull.SelectedColor = cdFull.Color;
        }

        private void btFullAccept_Click(object sender, RoutedEventArgs e)
        {
           cdFull.Color =cpFull.SelectedColor ;
           exFull.IsExpanded = false;
        }

        private void btFullCancel_Click(object sender, RoutedEventArgs e)
        {
              exFull.IsExpanded = false;
        }
        #endregion

        #region Full Alpha
        

        private void exFullAlpha_Expanded(object sender, RoutedEventArgs e)
        {
            cpFullAlpha.InitialColor = cdFullAlpha.Color;
            cpFullAlpha.SelectedColor = cdFullAlpha.Color;
        }

        private void btFullAlphaAccept_Click(object sender, RoutedEventArgs e)
        {
            cdFullAlpha.Color=  cpFullAlpha.SelectedColor;
            exFullAlpha.IsExpanded = false;
        }

        private void btFullAlphaCancel_Click(object sender, RoutedEventArgs e)
        {
            exFullAlpha.IsExpanded = 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)

Share

About the Author

KenJohnson
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by KenJohnson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid