Take the 2-minute tour ×

In my team, the code style in .xaml files are currently not very consistent. We looked over the Visual Studio auto formatter to make it format the code into something we like. However, for one option we lack an extra condition. The options Im talking about are found under Tools -> Options -> Text Editor -> XAML -> Formatting.

We want each attribute on a separate line. Initially, we also wanted the first attribute on a new line (below the start of the tag) like so:

    Attrib2="b" />

But we quickly realized that running with those rules would make tags that only set one attribute look horrible, especially if the were nested:

    Attrib1="a" />
        Attrib="b" />
            Attrib="c" />

So we tried positioning the first attribute on the same line as the start tag. While still keeping the two important rules (one attribute per line, vertically aligned). It looked decent in both cases:

<MyFooBarButton Attrib1="a"
                Attrib2="b" />

<MyFooBarButton Attrib1="a" />
    <NestedFoo Attrib="b" />
        <NestedFoo2 Attrib="c" />

The problem now is that Visual Studio seems to lack the condition to have both. I.e.:

  • If only one attribute is set: keep it on one line.
  • If more than one attribute is set: place first attribute on a new line, one attribute per line and align them vertically.

Can Visual Studio 2008 be made to do this? And if not, could the code formatter in ReSharper do it (might make it worth the cost)?

share|improve this question
add comment

2 Answers

Try http://xamlstyler.codeplex.com/, this xaml formatter won't break attributes into different lines, if an element has only 2 or less than 2 attributes, also, this formatter has the capability to sort the attributes of an element in a predefined rule, which make s the look of your markup much better.

share|improve this answer
add comment

maybe these will help you:




(last two links are mentioned in text under first link)

EDIT: I checked options in ReSharper and I couldn't find any options just for XAML formatting (there are for c#, vb.net and XML)

EDIT2: I checked formatting options in VS and there is something that seems exactly what you need. In "Spacing" there is option "Position each attribute on a separate line" and it has checkbox titled "Position first attribute on the same line as start tag". In my opinion it should do what you need.

share|improve this answer
Regarding "Position each attribute on a separate line", thats what I want assuming there is more than one attribute, otherwise not. I tried to explain the difference in my original question. Maybe I should revise it. –  mizipzor Mar 5 '10 at 10:26
what about that checkbox I mentioned? if you have only one attribute it should place it on the same line as tag and if there are more attributes they should be placed in separate lines due to option "position each attribute on a separate line". Maybe I don't understand something but desciption of these options suggests working according to your needs. –  grapkulec Mar 5 '10 at 12:53
and @mizipzor, the checkbox you mentioned does exactly what the OP wants, although I've only tested it at VS2010. –  Bruno Brant Nov 27 '10 at 17:56
add comment

Your Answer


