XAML Attributes on Separate Lines

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"
           Margin="10 0"
           Grid.Column="1" />

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 Options

Reformatting XAML

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

Posted 02-24-2010 5:41 PM by Christopher Bennage
Filed under: ,


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)