Property used for on color change

Aug 17, 2010 at 11:30 PM


This is a great color picker. I've used a few and this is the best one yet. Quick question: How do I handle a color change in the code behind? Or what propery name should I use in the XAML to trigger a method call in the code behind, e.g. see OnColorChanged below.









<CustomWPFColorPicker:ColorPickerControlView x:Name="ForeColorPicker" Height="17" Width="20" OnColorChanged="ChangeColor" />



Aug 18, 2010 at 6:56 PM
Edited Aug 18, 2010 at 6:58 PM

Solved! Just added a routed event handler code to  ColorPickerControlView. For those interested:

public static readonly RoutedEvent SelectedColorEvent =
            EventManager.RegisterRoutedEvent("SelectedColor", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(ColorPickerControlView));

        public event RoutedEventHandler SelectedColor
            add { AddHandler(SelectedColorEvent, value); }
            remove { RemoveHandler(SelectedColorEvent, value); }
        void RaiseSelectedColorEvent()
            RoutedEventArgs args = new RoutedEventArgs(ColorPickerControlView.SelectedColorEvent);

Also, and RaiseSelectedColorEvent(); as last line in SelectColorCommandExecute().

Then handle event in code behind of UserControl:

public UserControlX(){
    ForeColorPicker.SelectedColor += SetForeColor;

private void SetForeColor(object sender, RoutedEventArgs args)
            // Do something


Caveat: Does not raise event when selecting color from "More Colors" dialog. Should be a simple fix ... raise event at right place.