Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Daily news blogs about .NET and related creatures | Main | Resharper template for DependencyProperty in silverlight »
Wednesday
May272009

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.

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>