Dependence on DependencyObject and DependencyProperty

silverlight, xaml

Solution

Actually, in Silverlight you cannot inherit DependencyObjects, and so you should (and have to) implement INotifyPropertyChanged instead.

Implementing INotifyPropertyChanged has many advantages over DependencyObjects (I will abbreviate this DO to make it easier) and using DependencyProperties (DPs):

- This is more lightweight

- Allows you more freedom in modeling your objects

- Can be serialized easily

- You can raise the event when you want, which can be useful in certain scenarios, for example when you want to bundle multiple changes in only one UI operation, or when you need to raise the event even if the data didn't change (to force redraw...)

On the other hand, inheriting DOs in WPF have the following advantages:

- Easier to implement especially for beginners.

- You get a callback mechanism (almost) for free, allowing you to be notified when the property value changes

- You get a coercion mechanism with allows you to define rules for max, min and present value of the property.

There are other considerations, but these are the main.

I think the general consensus is that DPs are great for controls (and you can implement a CustomControl with custom DPs even in Silverlight), but for data objects you should rather implement INotifyPropertyChanged.

HTH, Laurent

Problem

I'm building a Silverlight application and one of my caveats from last time was that if you need anything done right in Silverlight/WPF way you'd need to model your objects as a DependecyObject and use DependencyProperty(ies) I find this model to be rather cumbersome, requiring static fields and initializers in half the classes I use, so is it a good idea to use the good-old event-driven (observer pattern?) in place of DependencyObject? I'm aiming to minimize code bloat and boiler plates (I hate them) and really would like to know if anyone with experience in Silverlight/WPF has any tips/techniques for keeping usage of DependencyObject and DependencyProperty to a minimum? Is this a good idea?

Original source