Link to home
Start Free TrialLog in
Avatar of OFGemini
OFGemini

asked on

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.


        <local:ColorPicker x:Name="cpicker" CurrentColor="{Binding AnnotationOutLineColor}"/>

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.



public class ColorPicker : Control
    {
        static ColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
        }


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

        /// <summary>
        /// Gets or sets the CurrentColor property.  This dependency property 
        /// indicates ....
        /// </summary>
        public Brush CurrentColor
        {
            get { return (Brush)GetValue(CurrentColorProperty); }
            set { SetValue(CurrentColorProperty, value); }
        }
        /// <summary>
        /// Handles changes to the CurrentColor property.
        /// </summary>
        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;
        }
    }


<Window x:Class="TestColorPicker.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestColorPicker"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <local:ColorPicker x:Name="cpicker" CurrentColor="{Binding AnnotationOutLineColor}"/>
        <Button Content="Button" Height="19" HorizontalAlignment="Left" Margin="32,46,0,0" Name="button1" VerticalAlignment="Top" Width="92"  Click="button1_Click"/>
    </Grid>
</Window>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CurrentColor="{Binding AnnotationOutLineColor, Mode=TwoWay}"