Dependency properties were some time ago introduced in WPF and are widely used in Silverlight 2 controls as well. They are in effect in e.g. animations, data-binding and styles. I'm sure you use them when creating a Silverlight 2 application. The need to write your own dependency properties will come when you decide to create your own Silverlight control. I'm not going to explain the dependency property concept here, you can find some good documentation on msdn.
The syntax is something you have to get used to, typically you will see something like this: (code includes the dependency property, a clr-wrapper for this property and a property-changed callback.
1: public class AppelControl : ContentControl
2: { 3:
4: public bool IsTest
5: { 6: get { return (bool)GetValue(IsTestProperty); } 7: set { SetValue(IsTestProperty, value); } 8: }
9:
10: public static readonly DependencyProperty IsTestProperty =
11: DependencyProperty.Register(
12: "IsTest",
13: typeof(bool),
14: typeof(AppelControl),
15: new PropertyMetadata(OnIsTestPropertyChanged));
16:
17: private static void OnIsTestPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
18: { 19: AppelControl _AppelControl = d as AppelControl;
20: if (_AppelControl != null)
21: { 22: //TODO: Handle new value.
23: }
24: }
25: }
Hmm.. a lot of work for a simple bool property, don't you think? But don't panic, we have codesnippets in Visual Studio, just type: propdp [TAB] and you get the following code:
1: public int MyProperty
2: { 3: get { return (int)GetValue(MyPropertyProperty); } 4: set { SetValue(MyPropertyProperty, value); } 5: }
6:
7: // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
8: public static readonly DependencyProperty MyPropertyProperty =
9: DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));
We ran into a small problem here, the codesnippet shipped is designed to work with WPF and not with Silverlight 2, this will not compile!
You can get a Silverlight 2 dependency property codesnippet over here. Just import this snippet and you can use propds [TAB] to get a full blown Silverlight 2 dependency property with just a few keystrokes.