66

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?

1
  • 1
    This issue was noted here: 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. Commented Jul 9, 2018 at 11:40

8 Answers 8

189

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow this is an amazing tip. I don't understand why this isn't the default though. (At least it wasn't for me).
This does not fix tabs
Good find! Would not have expected the default to preserve any spaces between attributes like in a verbatim style.
Brilliant tip, works a treat
23

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.

Comments

12

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

Comments

11

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

6 Comments

This does nothing in XAML VS2015 when I try it. In my menu it is shown as ^K^D which is mentioned in the original question and is noted not to work.
Worked for me as well.
in VS 2019 this menu option doesn't exist when in a xaml file. :(
This works in VS2019 now, at least, if not before.
Does not work for me in VS2022, maybe because of configuration differences? I ended up using an extension to fill the gap.
|
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.

Comments

2

I prefer to keep a tag with few attributes on a single line, but a tag with many attributes on multiple lines (each attribute on a separate line, as in stanimirsp's answer).

To doing so, I ended up with disabling of auto-formatting and using of hotkeys.

To disable auto-formatting, open Tools / Options / Text Editor / XAML / Formatting / General and uncheck all Auto-Formatting events.

Formatting hotkeys

  • To format selection or tag under cursor: Ctrl + K + F;
  • To format whole document: Ctrl + K + D.

Comments

1

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.