'How to automatically format XAML code in Visual Studio?

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?



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

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

Solution 5:[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.

Solution 6:[6]

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

Solution 7:[7]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Vimal Saifudin
Solution 4
Solution 5 Kacper Stachowski
Solution 6 mxmissile
Solution 7 Community