Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

Tuneo

ASP.NET Hosting transatlantys

Contact

Me

Others

Selected content

Archives

XAML markup formatting in Visual Studio

I'm using WPF these days, and so reading and writing a lot of XAML markup. As with any code or text, reading and navigation are made easier with a clean format. In my XAML files, I've been putting the XML attributes aligned vertically, below their XML element. It looks like this:

<ListBox
  Name="EntitiesListBox"
  BorderBrush="BlueViolet"
  Visibility="{Binding ElementName=EntitiesCheckBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"
  ItemContainerStyle="{StaticResource Style_ListBoxItem}"
  ItemsSource="{Binding EntityViews}"
  IsSynchronizedWithCurrentItem="True"
  AlternationCount="2">

Compare the above to the following, where the attributes are all on one line:

<ListBox Name="EntitiesListBox" BorderBrush="BlueViolet" Visibility="{Binding ElementName=EntitiesCheckBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" ItemContainerStyle="{StaticResource Style_ListBoxItem}" ItemsSource="{Binding EntityViews}" IsSynchronizedWithCurrentItem="True" AlternationCount="2">

The good news is that Visual Studio can help you to format your XAML markup without having to do too many manual adjustments. Laurent Bugnion shows how in a blog post. You simply have to change the default settings for XAML documents, and then use CTRL+E,D (Edit | Advanced | Format Document) or CTRL+E,F (Edit | Advanced | Format Selection).
The only thing different in my settings compared to Laurent's ones if that I uncheck "Position first attribute on same line as start tag".

While you're at it, you can check "Always open documents in full XAML view" (below Miscellaneous), if like me you're fed up with the painful designer, which is trying to display something by default.

Of course, it's all a matter of taste, and the choice is up to you.

PS: If you need more options, you can use the Dimebrain Markup Tamer addin.

Comments

karpach96 said:

I am trying to do opposite thing. When you switch between design view and code view, Visual Studio automatically does some re-formating. I don't this. Is there any way to disable this.

# November 2, 2008 5:23 PM