• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 998
  • Last Modified:

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

0
OFGemini
Asked:
OFGemini
1 Solution
 
apeterCommented:
Use your property to set the Colorpicker.currentColor like below
public Brush AnnotationOutLineColor
        {
            get { return (Brush)GetValue(AnnotationOutLineColorProperty); }
            set {
                         SetValue(AnnotationOutLineColorProperty, value);
                          CurrentColor = value;
                   }
        }
 
0
 
EDDYKTCommented:
CurrentColor="{Binding AnnotationOutLineColor, Mode=TwoWay}"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now