Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Understanding Custom Dependency Proeprties - Help Please! RSS

2 replies

Last post Dec 11, 2008 06:06 PM by ssawchenko

(0)
  • ssawchenko

    ssawchenko

    Member

    438 Points

    214 Posts

    Understanding Custom Dependency Proeprties - Help Please!

    Dec 11, 2008 04:20 PM | LINK

     Hello everyone, yesterday I created my first custom dependancy property on a custom control of mine... but it doesn't seem to be working as I expect and I'm wonding if I'm missing something, or if my understanding was incorrect.  I find the people on these forums tend to explain things better then MSDN articles and such ;) so I am here hoping someone can fill me in :)

     I have a custom control called "GraphicGrid" which consists of line elements that create a grid.  I wanted the user to be able to set the color of the grid via a dependancy property which I could hook up to a "PropertyEditor" user control that I also have created which accepts and writes back to DependancyProperties.  So I created the following on my GraphicGrid:

            public static readonly DependencyProperty GridLineStrokeProperty =
                 DependencyProperty.Register("GridLineStroke", typeof(Brush), typeof(GraphicGrid), null);

            public Brush GridLineStroke
            {
                get
                {
                    return (Brush)GetValue(GridLineStrokeProperty);
                }
                set
                {
                    // Color lines.
                    foreach(UIElement line in LayoutRoot.Children)
                    {
                        line.SetValue(Shape.StrokeProperty, value);
                    }

                    SetValue(GridLineStrokeProperty, value);
                }
            }

     

    Now, my understanding of a dependency property is that the public variable is DIRECTLY linked to the dependency property.  Perhaps this is where my understanding is lacking.   I put a break point in my variable set function and then I tried to set the property via the DependencyObject.SetValue function.

            this.SetValue(GraphicsGrid.GridLineStrokeProperty, new SolidColorBrush(Colors.Red));

    The breakpoint in my set function was never hit, but when I checked the value of the dependency property it had been changed. In order for the set function to be called I have to set the public variable directly for the set function to be called:

            this.GridLineStroke = new SolidColorBrush(Colors.Red);

    What I really wanted was to ensure that every time the GridLineStrokeProperty [dependency property] was changed, that all the lines in the grid were updated with the new color - WITHOUT having to set the public variable directly [because my PropertyEditor simply calls the SetValue function for the linked object with a given DependencyProperty and newValue]. 

    Am I going about doing this the wrong way?  Can someone shed some light, as I thought I for once knew what I was doing ;)

     

    Thanks in advance!

     

  • bryant

    bryant

    Star

    10113 Points

    1662 Posts

    Re: Understanding Custom Dependency Proeprties - Help Please!

    Dec 11, 2008 04:30 PM | LINK

    The trick with Dependency Properties is to set the PropertyChangedEventHandler. The reason you need to do this is that your property can be set using SetValue in which case your public property will not be hit.

     

    public static readonly DependencyProperty GridLineStrokeProperty =
                 DependencyProperty.Register("GridLineStroke", typeof(Brush), typeof(GraphicGrid), 
                 new PropertyMetaData(GridLineStrokeChanged));
    
    private static void GridLineStrokeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
       ((GraphicGrid)o).OnGridLineStrokeChanged((Brush)e.NewValue);
    }
    
    private void OnGridLineStrokeChanged(Brush newBrush)
    {
       GridLineStroke = newBrush;
    }
     Note: You may need to tweak my code as it is hand typed so the type names might be off.

    -- bryant

    Blog | Twitter
    _________________
    Dont forget to click "Mark as Answer" on the post that helped you.
  • ssawchenko

    ssawchenko

    Member

    438 Points

    214 Posts

    Re: Understanding Custom Dependency Proeprties - Help Please!

    Dec 11, 2008 06:06 PM | LINK

     Brilliant!  Thank you bryant!

    I modified your solution a little [I was getting some errors pertaining to static method calls], and it triggers perfectly now!

            public static readonly DependencyProperty GridLineStrokeProperty =
                DependencyProperty.Register("GridLineStroke", typeof(Brush), typeof(GraphicGrid), new PropertyMetadata(GridLineStrokeChanged));
            
            private static void GridLineStrokeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                GraphicGrid grid = o as GraphicGrid;  

                // Color lines.
                foreach (UIElement line in grid.LayoutRoot.Children)
                {
                    line.SetValue(Shape.StrokeProperty, e.NewValue);
                }
            }

            public Brush GridLineStroke
            {
                get { return (Brush)GetValue(GridLineStrokeProperty); }
                set { SetValue(GridLineStrokeProperty, value); }
            }

    I think because my DependencyProperty is registered to the GraphicGrid object type, that casting the sender to a GraphicGrid should be acceptable.  It was the only way I could seem to get access to the LayoutRoot in the static function.  If I'm incorrect in this please let me know :)

    Thank you so much for your help!