[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 986
  • 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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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