46

Here:

https://social.msdn.microsoft.com/Forums/de-DE/b77c7529-298f-4b9a-874a-f94f699986ac/automatically-formatting-xaml-code?forum=vswpfdesigner

... it is written that one can use "Ctrl+K+D" ... but that didn't work.

I also tried "shift + alt + F", which was suggested here:

How do you format code in Visual Studio Code (VSCode)

... it didn't work either.

So my question is: how can you automatically format XAML code in Visual Studio?

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • 1
    This issue was noted here: https://github.com/dotnet/roslyn/issues/16899 where it was suggested to report an issue using the VS feedback tool. I don't know if the OP of that post had done so, but FWIW I have. – StayOnTarget Jul 09 '18 at 11:40

7 Answers7

137

Probably your formatting options are wrong, go to: Tools -> Options -> Text Editor -> XAML -> Formatting -> Spacing and on Attribute Spacing section click "Position each attribute on a separete line" and then try "Ctrl+K+D" again.

stanimirsp
  • 2,548
  • 2
  • 26
  • 36
19

I would check out XAML Styler, which is a Visual Studio extension to help format your XAML source code (full disclosure, I am one of the owners on the project).

Edit: Forgot to mention that Visual Studio does not have a very rich set of XAML formatting capabilities on its own.

David Grochocki
  • 630
  • 4
  • 13
11

Shortcut key for XAML formatting in Visual Studio for Mac machines: Ctrl+I

Vimal Saifudin
  • 1,815
  • 1
  • 21
  • 28
9

In case you guys still don't know how...

It's called 'Format Document', you can find it inside menu:

Edit => Advanced => Format Document

There is also shortcut for it:

Ctrl+E then Ctrl+D

This works for most document, including XAML.

EDIT: When this post was made, I was using VS 2017

Wahyu
  • 4,203
  • 1
  • 19
  • 21
5

I use Xaml Formatter. Works pretty good for me. You will just have to assign code formatting with Xaml Formatter to a Ctrl+K Ctrl+D shortcut in Xaml files, since it is not done by default (it's creating Format Xaml option in Tools menu).

Also Inline Color Picker is a very simple but extremely useful extension if you are developing a lot of Xaml code.

1

Try using "XML Tools" for VS Code, the shortcut is Alt + Shift + F

mxmissile
  • 11,464
  • 3
  • 53
  • 79
user3916810
  • 43
  • 1
  • 8
0

For anyone who is searching for kind of combining "Insert a single space between attributes" and "Position each attribute on a seperate line" I found this solution:

Tools -> Options -> Text Editor -> XAML -> Formatting -> General -> Tag Wrapping

and tick "Wrap tags that exceed specified length".

Self-explanatory that one can adjust the length in the following input field.

Community
  • 1
  • 1