Follow @RoyOsherove on Twitter

A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

Having stated to look into silverlight not long ago, I was totally put off by the horridness that is DependencyProperties, and how you implement them:

  • You need to declare a static property that is registered at the type initialization phase
  • you need to have a “regular” property that calls getvalue and setvalue with that property
  • if you want to handle changes you need to registed a specific callback in the dependency declaration
  • the same goes for default value

yuck.

 

So here’s what I came up with. It’s a simple base class you can use in your silverlight projects, and inherit your custom control from. Once you do it, you can write DependencyProperties like this:

image

the things to note here:

  • You inherit from CustomControlBase
  • The On[PropertyName]Changed will be registered automatically based on its name (Convention based work)
  • You call GetDPValue and SetDPValue instead of GetValue and SetValue
  • You set the DefaultValue using an attribute (in System.ComponentModel)
  • You can omit the callback method if you want.

Daily news blogs about .NET and related creatures

Resharper template for DependencyProperty in silverlight