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.
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