ReSharper Code Snippet for Dependency Properties
January 17th, 2008
This is a ReSharper code snippet I’ve been using quite a lot lately – it allows you to easily create a WPF dependency property along with event handlers, documentation, and initialization code. Here’s a sample:
#region MyStringProperty dependency property /// <summary> /// This is a sample string property. /// </summary> public static readonly DependencyProperty MyStringPropertyProperty; //TODO: copy to static constructor //register dependency property //FrameworkPropertyMetadata md = new FrameworkPropertyMetadata("hello world", MyStringPropertyPropertyChanged); //MyStringPropertyProperty = DependencyProperty.Register("MyStringProperty", typeof (string), typeof (MyControl), md); /// <summary> /// A property wrapper for the <see cref="MyStringPropertyProperty"/> /// dependency property:<br/> /// This is a sample string property. /// </summary> public string MyStringProperty { get { return (string) GetValue(MyStringPropertyProperty); } set { SetValue(MyStringPropertyProperty, value); } } /// <summary> /// Handles changes on the <see cref="MyStringPropertyProperty"/> dependency property. As /// WPF internally uses the dependency property system and bypasses the /// <see cref="MyStringProperty"/> property wrapper, updates should be handled here. /// </summary> /// <param name="d">The currently processed owner of the property.</param> /// <param name="e">Provides information about the updated property.</param> private static void MyStringPropertyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MyControl owner = (MyControl) d; string newValue = (string) e.NewValue; //TODO provide implementation throw new NotImplementedException("Change event handler for dependency property MyStringProperty not implemented."); } #endregion
Just import the attached snippet into ReSharper, then type dpp
in Visual Studio to trigger the template.
BTW: If you’re using VS without ReSharper but need snippets for dependency properties, routed events or routed commands, Dr. WPF has the cure.
Download File: dpp-snippet.xml