WPF Dependency Property CodeRush template

I’m working on a WPF custom control and got tired of writing out the whole DependencyProperty every time so added a new CodeRush template to write the DependencyProperty registration, the read/write property using GetValue and SetValue, and an OnPropertyChanged static callback method. Here’s the template text:

«:#PropertyDefaultScope#»«TypeLink(“«?Get(Type)»”)» «Caret»«FieldStart»«Link(MyProperty)»«FieldEnd»«BlockAnchor»
{
get { return («TypeLink(“«?Get(Type)»”)»)GetValue(«Link(MyProperty)»Property); }
set { SetValue(«Link(MyProperty)»Property, value); }
}

public static DependencyProperty «Link(MyProperty)»Property = DependencyProperty.Register(
“«Link(MyProperty)»”, typeof(«TypeLink(“«?Get(Type)»”)»), typeof(«TypeName»),
new PropertyMetadata(«FieldStart»null«FieldEnd», new PropertyChangedCallback(On«Link(MyProperty)»«FieldStart»«Link(Changed)»«FieldEnd»)));

private static void On«Link(MyProperty)»«Link(Changed)»(DependencyObject dObj, DependencyPropertyChangedEventArgs e)
{
«Target»
}

It links the property type and name, callback method name, and fills in the parent class type. I set the generic template name to dp?Type? which isn’t otherwise used. This will save you a whole lot of typing if you’re working with WPF. Note: Workflow also uses Dependency Properties but they are based in a different namespace and slightly different syntactically.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>