A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase - ISerializable - Roy Osherove's Blog

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.
Published Wednesday, May 27, 2009 8:44 AM by RoyOsherove
Filed under: ,

Comments

Wednesday, May 27, 2009 9:20 AM by David Taylor

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

Wow - That is great Ray!

I am not doing WPF/Silverlight at the moment but bookmarked your link as it is a great simplification.

This is the kind of thing that bugs you because you might only need to do if every few months....and end up reading lots of MSDN docs and wasting a lot of time.  Thanks.

Dave

Wednesday, May 27, 2009 9:42 AM by wekempf

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

The biggest problem I see with this is that you no longer have access to the DP itself. That removes a lot of the functionality for which DPs were created to begin with.

Wednesday, May 27, 2009 9:52 AM by RoyOsherove

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

eran, you mean in design time when writing xaml?

Wednesday, May 27, 2009 10:30 AM by wekempf

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

"eran, you mean in design time when writing xaml?"

Eran? Were you asking this about my response?

In WPF you use the DP for several things. One, you can borrow a DP from another class without having to inherit from that class, which you won't be able to do here. You can use the DP to modify the metadata, for things like listening to the property changes externally. This solution won't work for attached DPs.  I'm sure there's other details I'm missing.

Maybe a variation would be better:

public class CustomControl

{

 public static DependencyProperty TextProperty = DependencyPropertyHelper.Register(typeof(CustomControl), "Text");

 [DefaultValue("hello")]

 public string Text

 {

    get { return (string)GetValue(TextProperty); }

    set { SetValue(TextProperty, value); }

 }

 private static void OnTextChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)

 {

 }

}

This doesn't require a base class, still exposes the DP, and uses convention based registration.  It's lacking a few details, such as being able to specify PropertyMetadata (something that's anemic in Silverlight, but important in WPF), and I'm uncertain that the sugar is all that important, but it's an interesting idea none-the-less.

Wednesday, May 27, 2009 11:45 AM by Gabor

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

He means you don't have access to the static property (CustomControl.TextProperty). You would need that for attached properties and for some other scenarios. Still, you should be able to have CustomControlBase.GetProperty("Text") as a static method that does it for you.

Wednesday, May 27, 2009 8:41 PM by DotNetShoutout

# A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase - Roy Osherove

Thank you for submitting this cool story - Trackback from DotNetShoutout

Wednesday, May 27, 2009 9:56 PM by Justin Angel

# re: A simpler, conventions based DependencyProperty structure for silverlight using CustomControlBase

Hi Roy,

This code won't work under the context of the Silverlight CLR.

You're using private reflection to get it to work, and private reflection is not allowed in the Silverlight CLR.

And as one of the previous posters noted, this does mean losing on of the biggest gains in C# DPs - the ability to use property references.

That said, it's a pretty cool idea.

I'd be more in favour of DP pre-compile generation rather than runtime-instantiation, but either way - this is a very solid step in the right direction.

-- Justin Angel