ASP.NET Hosting

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.

1 Comment

Comments have been disabled for this content.