views:

180

answers:

2

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:

<MyFooBarButton
    Attrib1="a"
    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:

<MyFooBarButton
    Attrib1="a" />
    <NestedFoo
        Attrib="b" />
        <NestedFoo2
            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)?

+1  A: 

maybe these will help you:

http://weblogs.asp.net/fmarguerie/archive/2008/06/07/xaml-markup-formatting-in-visual-studio.aspx

http://geekswithblogs.net/lbugnion/archive/2007/09/29/Cleaning-up-XAML-code-and-improving-parsing-time-with-Blend.aspx#lbu_note2

http://dimebrain.com/2008/05/automating-read.html

(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.

grapkulec
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
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
+1  A: 

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.

chris

related questions