Feed Icon  

Contact

  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics

Tags

Open Source Projects

Archives

Ads

Silverlight Dependency Properties

Posted in Silverlight at Monday, December 15, 2008 2:08 AM Pacific Standard Time

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.

Monday, December 15, 2008 5:30:56 AM (Pacific Standard Time, UTC-08:00)
Why not implement INotifyPropertyChanged and use that as the callback?

http://solutionizing.net/2008/10/24/inotifypropertychanged-dependency-properties-for-silverlight-pathtextblock/
Monday, December 15, 2008 5:52:51 AM (Pacific Standard Time, UTC-08:00)
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.
Bryant Likes
Monday, December 15, 2008 6:46:27 AM (Pacific Standard Time, UTC-08:00)
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
Monday, December 15, 2008 9:34:17 AM (Pacific Standard Time, UTC-08:00)
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!
Bryant Likes
Tuesday, December 16, 2008 5:09:02 PM (Pacific Standard Time, UTC-08:00)
Thanks!

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

Greets,
Jonathan
Tuesday, December 16, 2008 6:21:38 PM (Pacific Standard Time, UTC-08:00)
Here is a full blown explanation of dependency properties and routed events.

http://www.deepinconcept.net/post/2008/10/31/Silverlight-20-Dependency-Properties-And-Routed-Events.aspx
Friday, January 16, 2009 3:26:11 AM (Pacific Standard Time, UTC-08:00)
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
Andy B
Comments are closed.