Bryant Likes's Blog

It's all about WebData

Recent Posts

Tags

News


  • Windows Live Alerts
    View Bryant Likes's profile on LinkedIn

    Me

    Get Microsoft Silverlight
    by clicking "Install Microsoft Silverlight" you accept the
    Silverlight license agreement


    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are the personal opinions of the individual authors and do not represent the views of Avanade in any way.

Community

Email Notifications

Archives

Silverlight Dependency Properties

I’ve seen this question a number of times in the Silverlight Forums, so instead of just answering it one more time I decided to answer it here so I can refer back to this. The question generally looks like this:

I have create a dependency property and the setter isn’t getting called during data binding. My dependency property is declared as follows:

public class MyClass : Control

{

 

 

    public int MyProperty

    {

        get { return (int)GetValue(MyPropertyProperty); }

        set

        {

            SetValue(MyPropertyProperty, value);

            // some custom code here

        }

    }

 

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...

    public static readonly DependencyProperty MyPropertyProperty =

        DependencyProperty.Register("MyProperty", typeof(int), typeof(MyClass), new PropertyMetadata(0));

 

 

}

 

The reason why the setter isn’t called here (at least the way I understand this), is that when data binding sets this value it isn’t calling the public property. Instead it is using the SetValue on the dependency object instead. So the custom code is never called. What you need to do is add a PropertyChangedCallback to the DependencyProperty registration as follows:

public int MyProperty

{

    get { return (int)GetValue(MyPropertyProperty); }

    set

    {

        SetValue(MyPropertyProperty, value);

    }

}

 

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...

public static readonly DependencyProperty MyPropertyProperty =

    DependencyProperty.Register("MyProperty", typeof(int), typeof(MyClass), new PropertyMetadata(0, MyPropertyChanged));

 

private static void MyPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)

{

    ((MyClass)o).OnMyPropertyChanged((int)e.NewValue);

}

 

private void OnMyPropertyChanged(int newValue)

{

    // custom code goes here instead

}

 

 

Now whenever the property changes the OnMyPropertyChanged method will get called. Notice that this is where you put your custom code to handle the property changing. This code will get called when the public property setter is called or when the property is changed using the SetValue method.

More information on Dependency Properties can be found here.

Comments

Keith Dahlby said:

Why not implement INotifyPropertyChanged and use that as the callback?

solutionizing.net/.../inotifypropertychanged-dependency-properties-for-silverlight-pathtextblock

# December 15, 2008 1:30 PM

bryantlikes said:

Sorry, read that link but I don't see how that relates. INotifyPropertyChanged is more for classes that you bind to, not the control that is hosting them. I don't see what you would gain from implementing that interface on a control.

# December 15, 2008 1:52 PM

Keith Dahlby said:

I guess I misread your intent, apologies. Instead, I resubmit the implementation of INotifyPropertyChanged as a useful example of custom code one would want executed when a DP is set - using the method you describe. :)

As for why a Control might want to implement the interface, there are a number of reasons. For example, binding an interface element to the value of a property on your control - in my case, displaying the current parameters for the PathTextBlock.

Cheers ~

Keith

# December 15, 2008 2:46 PM

bryantlikes said:

Thanks for the useful example! :)

I'll dig into your code a little more, I think I'm starting to see what you're doing. I'll have to dig into that PathTextBlock as well.

Thanks again!

# December 15, 2008 5:34 PM

Community Blogs said:

In this issue: Boyan Mihaylov, Jeff Prosise, Tim Heuer, Bryant Likes, Page Brooks, Michael Wolf, Jeff

# December 16, 2008 10:18 AM

Jonathan van de Veen said:

Thanks!

Finally someone who explains it in a short and understandable way.

Greets,

Jonathan

# December 17, 2008 1:09 AM

Joseph Ghassan said:

Here is a full blown explanation of dependency properties and routed events.

www.deepinconcept.net/.../Silverlight-20-Dependency-Properties-And-Routed-Events.aspx

# December 17, 2008 2:21 AM

#.think.in said:

#.think.in infoDose #12 (15th Dec - 19th Dec)

# December 21, 2008 2:48 PM

Andy B said:

Yikes, I understood that - and got it working too :-)

Thanks! I was about to give up!!!! BTW I found the new value (which I was creating via a xaml animation) was on the getter. So that...

private void OnMyPropertyChanged()

{

   // custom code goes here instead

   int value = MyProperty;

}

No doubt SL3 will change everything!!!!

Andy B - in cloudy Lundun Town

# January 16, 2009 11:26 AM

Kelly Brown said:

Original post by Dmitri Gromov

# June 12, 2009 12:42 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)