troubleshooting Question

binding to a dependency property on a custom control

Avatar of OFGemini
OFGemini asked on
.NET ProgrammingMicrosoft Development
2 Comments1 Solution1012 ViewsLast Modified:
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>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros