'How to toggle a line comment in XAML for C# WPF in Visual Studio 2019

I have Ctrl+Q set to toggle line comments globally in my C# project.

It does works when I'm editing .cs file.

Editor.ToggleLineComment in Visual Studio Options.

However, it does not work when editing XAML files.

Sample XAML file.

It does work if I press the comment and uncomment buttons

Visual Studio toolbar with comment and uncomment buttons.

But not with the hotkey I set.

Is there a different command I should use for toggling the XAML comment wrapper with a single hotkey?



Solution 1:[1]

It seems that the XAML editor does not use the Editor.ToggleLineComment shortcut. Instead it is:

  • Editor.CommentSelection for commenting a line or selection in XAML
  • Editor.UncommentSelection for uncommenting a line or selection in XAML

When pressing the shortcut, the XAML line will be commented out, if the cursor is placed anywhere in the line (no selection) or the selected characters are commented out, if you select a range of text.

Solution 2:[2]

@John.

You can install Toggle Comment to make Editor.ToggleLineComment work in XAMl code. Below is the Step:

  1. Download and install Toggle Comment from here
  2. Restart VS.
  3. Go to "Tools>Options...>Environment>Keyboard" and search for "ToggleLineComment" then bind it to your favorite key
  4. Open XAML code page, "Edit" -> "Advances" -> "Toggle Comment" to make the Menu Placement.

Update:

The result of the fourth step is shown in the picture below: (What we need to note here is that the fifth step is executed in the XAML page)

enter image description here

Solution 3:[3]

For anyone still looking for the answer, I found the solution (Working with VS 2022)

  1. Open keyboard shortcuts enter image description here

  2. In shortcut dialog box, find and select Editor.CommentSelection

  3. Then select Text Editor under Use shortcut in: and set the shortcut key Ctrl+/ (in my case)

  4. Assign the key enter image description here

  5. Repeat the process for Editor.UncommentSelection

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 thatguy
Solution 2
Solution 3 tayyab