39

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
  • 2,661
  • 9
  • 28
  • 59
  • 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

108

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
  • 1,928
  • 2
  • 21
  • 31
  • 2
    Wow this is an amazing tip. I don't understand why this isn't the default though. (At least it wasn't for me). – Peter Moore Dec 04 '21 at 19:26
17

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
  • 580
  • 4
  • 13
11

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

Vimal Saifudin
  • 1,665
  • 18
  • 26
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
  • 2,813
  • 1
  • 16
  • 20
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
  • 10,827
  • 3
  • 50
  • 78
user3916810
  • 33
  • 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