Buried deep within the options for Visual Studio is a little switch that places your XAML attributes on separate lines. For example, instead of looking like this:
<TextBlock Text="Some Text" Margin="10 0" Grid.Column="1" />
Your XAML would look like this:
<TextBlock Text="Some Text"
But Christopher, why do I care?
I initially started using this option because it made code samples easier to read (especially when going to print). However, a more valuable reason is that this practice is source control friendly.
In particular, it makes understanding the delta in files much easier. Frequently, revising XAML means adjusting the values of the attributes, adding new attributes, and removing attributes. Keeping attributes on separate lines makes it much easier to both identify and isolate changes. (Which is very helpful when merging conflicts).
Enabling the Option
Here’s how to turn it on in both Visual Studio 2008 and 2010:
- On the menu bar, Tools | Options
- Navigate in the tree to Text Editor | XAML | Formatting |Spacing
Visual Studio will attempt to respect this option after you’ve enabled. However, sometimes you’ll want to force some XAML to be reformatted explicitly.
On the menu bar, when you are editing XAML, select
Edit | Advanced | Format Document
The keyboard shortcut is Ctrl+K, Ctrl+D. (I really hope that’s the default shortcut).