XAML Formatter
# Sunday, February 27, 2011

I’ve been doing a lot of XAML work lately on a WPF project, and the client is really concerned about sloppy / ugly XAML.  The one problem I found was that Visual Studio 2010 is not really all that great at formatting XAML and it certainly does not format it the way I want it formatted.  So I decided to write something that would do the formatting for me, I started with a PowerShell script that I used via PowerConsole.  Worked great, but with some free time I decided why not write a proper VS2010 extension.

The key principle behind the formatter is that it only concerns itself with indentation, not what should be on any given line.  Visual Studio does offer some options in this regard but the closest thing I could get to what I wanted meant that when I formatted, a typical style Setter was 2 or 3 lines.  Multiplying the size of a style by 2 or 3 times makes for a rather large XAML file, not ideal.  So you worry about what goes on what line, and I’ll take care of making it all nicely aligned. 

Comment indentation is not perfect yet, but it usable, maybe v1.1 will have some improvements.

I’m looking into V2 which will simply intercept the Format Code command.  I didn’t go this way in V1 because of the complexity around formatting a selection.  Currently I format the entire document.  When I finish V2 I’ll post both code samples.

NOTE: I originally wrote this extension back in early October 2010, so there might be a similar tool available now.

BEFORE:

PreFormatting

AFTER:

PostFormatting

Feedback: I’d love to hear some feedback, if you have something that does not format correctly and feel comfortable sending me the XAML snippet, please do.   Any questions, comments, or concerns can be sent to XamlFormatter@danno.ca

Download: You can download the latest version of the XamlFormatter from the Visual Studio Gallery

Sunday, February 27, 2011 4:50:48 PM (Eastern Standard Time, UTC-05:00)  #    Comments [4]Trackback
Tuesday, May 03, 2011 3:51:54 PM (Eastern Daylight Time, UTC-04:00)
Sounds like a good idea, but it does not work for me. It indents some lines more than others that should be aligned together.

In the end, my closing Window tag is 2,532 spaces indented!
Thursday, July 21, 2011 10:59:25 AM (Eastern Daylight Time, UTC-04:00)
very usefull
Thanks.
Thursday, August 02, 2012 3:35:25 AM (Eastern Daylight Time, UTC-04:00)
Thanks for sharing!

It's especially useful to me since it can also be used in VS2012, but I have to admit that another plugin, XAML Styler (in VS2010), is a similar and more feature rich tool.
For XAML Styler, see: http://visualstudiogallery.msdn.microsoft.com/d6634d0e-38fb-48b6-829f-dadbc5c2fb62.
Tuesday, November 13, 2012 4:52:04 AM (Eastern Standard Time, UTC-05:00)
Hello

It does not work for me. I have installed it from Visual Studio Gallery and when I chose Format XAML, the button becomes disables, but nothing is aligned in the xaml.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview