Christopher Bennage

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

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: ,

[Advertisement]

Comments

Walt Ritscher wrote re: XAML Attributes on Separate Lines
on 02-24-2010 11:19 PM

I completely agree with this, I have been using multiline formatting for several years now.  I find it much easier to read when arranged like this.  Sometime I wish there was a way to sort them too.

Fabrice wrote re: XAML Attributes on Separate Lines
on 02-26-2010 5:13 AM

It also makes copying & pasting easier. If you want to copy attributes, you can just copy one or several lines. The same for deleting attributes.

This works better if you don't put the first attribute on the same line as the start tag though.

Khalil Muhammad wrote re: XAML Attributes on Separate Lines
on 03-03-2010 5:00 AM

Well written. I totally agree with you. Using such a formatting scheme makes it easier to deduct the logical structure of UI elements defined in the XAML.

uberVU - social comments wrote Social comments and analytics for this post
on 03-06-2010 11:48 PM

This post was mentioned on Twitter by ke4ktz: GReading: XAML Attributes on Separate Lines http://goo.gl/fb/UjBf

Steve Crane wrote re: XAML Attributes on Separate Lines
on 03-08-2010 7:19 AM

A nice option that I would like to suggest to my colleagues. One question though; is there a similar option in Expression Blend? The bulk of our XAML is created by a designer and there's little point turning this on for VS if Blend can't do the same thing.

Christopher Bennage wrote re: XAML Attributes on Separate Lines
on 03-08-2010 9:54 AM

@Steve Unfortunately, Blend does not have this feature. (At least, I can't find it). I spend a lot of time in Blend myself, and I usually keep the same files open in VS so that I can reformat them there. Far from ideal ...

Christopher Bennage wrote re: XAML Attributes on Separate Lines
on 03-08-2010 4:43 PM

Here's the place to encourage the Blend tend to support this:

expressionblend.uservoice.com/.../500231-option-to-position-each-attribute-on-a-separate-li

Microsoft Weblogs wrote Windows Client Developer Roundup for 3/8/2010
on 03-11-2010 4:11 PM

This is Windows Client Developer roundup #14. This is my first Roundup on my new blogging platform .

Delay's Blog wrote Breaking up (lines) is (not) hard to do [Tip: Put XAML attributes and elements on multiple lines so the markup is easy to read and work with]
on 06-24-2010 1:09 PM

Tip Put XAML attributes and elements on multiple lines so the markup is easy to read and work with Explanation

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
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

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)

CodeBetter.Com