Over a million developers have joined DZone.

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

 

Topics:

Published at DZone with permission of Christopher Bennage, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}