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]

This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of

See you there!

Comments (12)
  1. Morten says:

    Personally I don't like to have too many line feeds, since vertical scrolling will just hurt you instead of the horizontal scroll, so I'd like to keep related properties next to each other on the same line, for instance alignments on one, Padding and Margin on another, Grid.Row/Grid.Column, Width/Height  etc. Also x:Name gets to go up the same line as the opening tag. I try to keep the textwidth from exceeding 110-120 characters, and use the column guides to help with that.

    However, I do like your revision control argument. On the other hand, my compare tool does inline difference highlighting.

  2. David Anson says:


    Fair points, all of them! 🙂 In my experience, vertical scrolling is a fact of life, but horizontal scrolling is somewhat optional. By biasing formatting towards vertical scrolling, it can help keep horizontal scrolling out of the mix most of the time. And while I agree that good diff-ing tools will show intra-line differences, I often find myself working in environments where that doesn't happen. At the end of the day, it's all a bit of wishy-washy personal preference – these are just some techniques that I've grown fond of.

  3. Marc Roussel says:

    My opinion only.  I hope I will never have to write any xaml by hand.

  4. Mark says:

    My opinion only.  I hope I will always be able to write xaml by hand.  Many activities are quicker than can be done by mousing which I assume is what you would prefer.

  5. David Anson says:


    I hope you both get your wish – but for most people, I think the reality is probably somewhere in between… 🙂

  6. Rob says:

    I write lots of XAML by hand so I totally agree except,

    remember that xaml does not get stripped of white space when added to the xap and services like bing map apps require all xaps to be under 100k.

    This is especially true of comments that also not stripped.

  7. Jim says:

    I have found that in certain places in Xaml this is worthwhile. What works better is to change the color settings in VS so that the attributes and values 'pop' more. This keeps you from having to go back and edit a lot of UI stuff more than necessary.

  8. Jobi says:

    Nowadays all of us have wide screen display for development, So you will really need to utilize the screen area and minimize the scrolling. I second Morton's point of putting all usual/common attributes like Margin,Grid.Column etc on a single line horizontally. Most of those we(As a developer) don't care, and in Expression-Blend you dont really need to read those XAML. But put all Binding and other uncommon Attributes in new lines.

  9. David Anson says:


    What you say is true, though it's been my experience that the default (ZIP) compression that's applied to the content of XAP files really tends to minimize the impact of extra XAML whitespace on the final download size. There's some value in "minifying" the XAML for public assemblies, but I'm not aware of any great infrastructure to do so at this time – possibly because the benefit is relatively low in the overall scheme of things. However, if you find out about a good option here, I'd love to know about it! 🙂

  10. Colin E. says:

    Hi David,

    Looks neat to me, but what about bindings? They can get pretty verbose with Path, Converter (plus StaticResource referencer), ConverterParameter et. al.

    However, they look a bit funny when spread across multiple lines.

    How do you handle these?

    Colin E.

  11. David Anson says:


    I tend to create Bindings using the conventional markup extension form (Property="{Binding …}") and I leave everything on the same line. I realize that in some cases the Binding can get pretty complicated, but in my mind the whole line is generally doing just one "thing" (like setting the TextBlock's Text property). So while I realize that specific aspects of the Binding can change independently – thereby defeating some of the point of my revision control argument – this hasn't been much of an issue for me so far.

    However, (I know *you* know this, Colin, but some folks might not be aware) Bindings can also be specified with object element syntax (<Binding …/>), so that's definitely an option for people who want to take my recommendation to the extreme. 🙂…/ms752300.aspx

  12. akjoshi says:

    VS itself provides very good support for formatting(auto) XAML. You just need to set few settings and that's it, you can easily format your XAML by using one shortcut and moreover if you copy paste the code VS automatically formats it.

    Auto formatting the XAML :…/auto-formatting-the-xaml.aspx

Comments are closed.

Skip to main content