Saturday, July 05, 2008   Search 
Links
ItemsControl:  A to Z
(but not necessarily in that order)

 

Disciple
 My WPF Code Snippets Minimize
Location: BlogsAsk Dr. WPF    
Posted by: Dr. WPF 12/17/2007 9:32 AM

Yikes!  Has it really been over a month since my last blog entry!?!

Okay, the last 6 weeks are a bit hazy and I wish I could blame my absence from the WPF Forum (and this blog) on something exciting, but honestly, it's just been work.  A number of projects all converged at once creating "the perfect storm" of work engagements.  And although I love writing WPF code, I'm now hoping for a small respite from the daily deadlines.  Hopefully, the next few weeks will be a little more tame and I will be able to catch up with life in the WPF community.

Whenever we go through these crunch times, I realize how much I've come to depend upon the code snippet support in Visual Studio 2005/2008.  I totally rely on my WPF code snippets... and its not just because of the time they save me writing code, but also because of the consistency they bring to my code.  I can look at any WPF classes I've written over the past few years and immediately understand what is going on in the properties, events, and commands exposed by those classes.

If you do not yet have a good set of Visual Studio code snippets, I would encourage you to develop them.  I have posted my C# WPF snippets here for anyone who is interested in perusing, adopting, or improving them. 

<UPDATE>

January 4, 2008:  The downloadable snippets file now contains a .vsi file that can be used to directly import these snippets into Visual Studio 2005/2008.  (Special thanks to the coworker who was nice enough to create the install package for me!)

</UPDATE>

(Apologies to the VB.NET WPF developers out there...  I have never ported these to VB, as the time I spend writing VB code is extremely limited.  But if anyone is up for a challenge and wants to port these and send them my way, I'd be happy to post the equivalent VB snippets on my site.)

I have designed these snippets to cover 98% of the usage scenarios that I encounter in a typical WPF development project.  I have also designed them to enforce good coding patterns, especially around consistency and documentation.

I won't spend a lot of time explaining how to use them (because hopefully they are self-explanatory for WPF developers).  There are really only three shortcut keywords to remember:

  • dp  (for dependency properties)
  • rc  (for routed commands)
  • re  (for routed events)

From there, it's just a matter of choosing the correct snippets from the context menu.  I've found that I can now invoke most of my snippets without even thinking about the keystrokes... my fingers just go into "auto" mode ( d - p - - - 2 - - property name - property type - ... ). 

Of course, I created the blasted things, so maybe I'm not a representative sample. 

In addition to the WPF-specific snippets, I've included a couple of other snippets in this zip that I also use quite a bit... "pc" provides the PropertyChanged implementation for the INotifyPropertyChanged interface and "pcp" is used to define a property that raises such a change notification. 

I'm looking forward to a day when I'll be able to do more advanced things in my snippets (custom functions, custom formatting, capitalization, custom placement for different code parts, etc).  In the meantime, I hope others will find my existing snippets useful!

Cheers,
Dr. WPF

Permalink |  Trackback

Comments (11)   Add Comment
Re: My WPF Code Snippets    By Rick Eberle on 12/18/2007 9:16 AM
These snippets are great. Although I could use them in VB more. Let us know if anyone ever ports them over.

Re: My WPF Code Snippets    By Kent Boogaart on 12/19/2007 12:02 PM
The defaults IsHilarious :)

Re: My WPF Code Snippets    By Jeremiah Morrill on 12/19/2007 1:45 PM
Was starting to wonder what the hell happened to you in the forums. At least it's given other people a chance to build up some forum points without you bogartin all the questions! =P

-Jer

Re: My WPF Code Snippets    By Marlon Grech on 12/20/2007 5:34 AM
Cool set of snippets.... Thanks

Re: My WPF Code Snippets    By Mike Brown on 1/11/2008 8:35 AM
I made some PC and PCP snippets as well. Looks about the same as yours. Good to have you back...was wondering if the aliens had come to take you back home :P

Re: My WPF Code Snippets    By Philipp on 1/17/2008 6:13 AM
It's only for dependency properties, but here's a snippet for the ReSharper folks, too:

http://www.hardcodet.net/net/wpf/2008/01/resharper-code-snippet-for-dependency-properties

Re: My WPF Code Snippets    By Denis Vuyka on 4/11/2008 11:33 AM
Thanks for sharing this stuff. It's worth of gold.

Re: My WPF Code Snippets    By Roland Rodriguez on 6/30/2008 2:00 PM
Hi Doc,

Love your blog, love the snippets. Just wondering if I'm doing something wrong, the routed event helper methods have more arguments than the calls to them from the generated routed events. Is this a feature or a bug? ;-)

Regards,
Roland Rodriguez
http://WPFBlog.info

Re: My WPF Code Snippets    By John "Z-Bo" Zabroski on 6/30/2008 4:48 PM
Is there a way to write code snippets as if they were macros?

What I really want is for in XAML to be able to write stuff like:

grid(r=4,c=4) TAB TAB
generates
<Grid>
<Grid.RowDefinitions>
... four rows
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
... four columns
</Grid.ColumnDefinitions>
<Grid>

and then a macro like:
#5 Label, TextBlock TAB TAB
generates
<Label x:Name="$lblName0$"/>
<TextBlock x:Name="$Name0$"/>
etc...

a fellow co-worker tells me VS sucks at really powerful stuff like this and to just use PowerShell Get-ClipBoard and Set-ClipBioard. I'd rather not depend on an external tool unless I can integrate PoerShell with VS

Re: My WPF Code Snippets    By Dr. WPF on 6/30/2008 2:11 PM
Hey Roland,

Glad you like the snippets!

If you're referring to the AddHandler() and RemoveHandler() helper methods, these are actually correct. They should have 3 arguments. These helpers are only accessed by the attached routed events. There are similarly named methods (with only 2 arguments) inherited from UIElement, which are called by the non-attached routed events.

The RaiseEvent() helper, on the other hand, is accessed by both the attached and non-attached routed events.

Hope this helps!

Cheers,
-dw

Re: My WPF Code Snippets    By Dr. WPF on 6/30/2008 4:53 PM
Hi Z-Bo. Currently, there is no support for the macro style snippets that you describe. I'm not really "in the know" when it comes to future snippet support/features within VS.


Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 

  
Copyright 2007 by Dr. WPF   Terms Of Use  Privacy Statement