It is very unfortunate that the comment feature is not smarter than this when it comes to a block that already contains some commented out lines in XML.
A fairly painless workaround to this problem can be to use regular expressions:
- Select the block of XAML code you want to comment out.
- Click on the comment button from Visual Studio tool bar
- Keeping your commented out block of text selected:
- Open the Find/Replace dialog box (CTRL + SHIFT + H)
- In the Find Options, select the "Use regular
expression" check box.
- Ensure the "Look In:" combo box is set with
"Selection".
- In your "Find" field, enter:
\<\!\-\-(.*)\-\-\>
- In your "Replace" field, enter:
--><!--$1--><!--
- Click the "replace all" button
This will wrap any commented out lines within your block with the closing comment tag at the begining and the opening comment tag at the end, ensuring the block of text preceding this comment is valid and the one following it is too.
To remove the comments and return to your original block of XAML, use the regular expression first, but with the reverse logic:
- Find field:
\-\-\>\<!\-\-(.*)\-\-\>\<\!\-\-
- Replace field:
<!--$1-->
Then, keeping the block of XAML selected, click the Uncomment button from Visual Studio.
NOTE: Depending on the version of Visual Studio you are using, the syntax of the regular expression may vary. I am using VS 2012. Previous versions would use the curly braces '{}' to isolate an expression and the backslash '\' to use it back in the replace field. Now, it is the parenthesis '()' and the dollar sign '$', respectively.