Question : binding to a dependency property on a custom control

I created a Custom control called color picker with one dependency property called CurrentColor and then I placed this control on a window.  This window has a dependency property called AnnotationOutLineColor.

Now I'm trying to bind the window's AnnotationOutLineColor dependency property to the Color Picker control's CurrentColor but changes to AnnotationOutLineColor aren't modifying ColorPicker.CurrentColor.


       

How can I bind these properties together so that changes to AnnotationOutLineColor update ColorPicker.CurrentColor property?  I know I change Colorpicker's current color property in the code behind in a ColorChanged event but I want to know how to do this through Binding.


Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
public class ColorPicker : Control
    {
        static ColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
        }


        /// 
        /// CurrentColor Dependency Property
        /// 
        public static readonly DependencyProperty CurrentColorProperty =
            DependencyProperty.Register("CurrentColor", typeof(Brush), typeof(ColorPicker),
                new FrameworkPropertyMetadata((Brush)Brushes.White, FrameworkPropertyMetadataOptions.AffectsRender,
                    new PropertyChangedCallback(OnCurrentColorChanged)));

        /// 
        /// Gets or sets the CurrentColor property.  This dependency property 
        /// indicates ....
        /// 
        public Brush CurrentColor
        {
            get { return (Brush)GetValue(CurrentColorProperty); }
            set { SetValue(CurrentColorProperty, value); }
        }
        /// 
        /// Handles changes to the CurrentColor property.
        /// 
        private static void OnCurrentColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }



    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public Brush AnnotationOutLineColor
        {
            get { return (Brush)GetValue(AnnotationOutLineColorProperty); }
            set { SetValue(AnnotationOutLineColorProperty, value); }
        }

        public static readonly DependencyProperty AnnotationOutLineColorProperty = DependencyProperty.Register(
            "AnnotationOutLineColor",
            typeof(Brush),
            typeof(Window1),
            new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnAnnotationOutLineColorChanged)));

        private static void OnAnnotationOutLineColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.AnnotationOutLineColor = Brushes.Blue;
        }
    }



    
        
        

Answer : binding to a dependency property on a custom control

Use your property to set the Colorpicker.currentColor like below
public Brush AnnotationOutLineColor
        {
            get { return (Brush)GetValue(AnnotationOutLineColorProperty); }
            set {
                         SetValue(AnnotationOutLineColorProperty, value);
                          CurrentColor = value;
                   }
        }
 
Random Solutions  
 
programming4us programming4us