Is there a way to comment out XAML that contains comments? Is there a way to comment out XAML that contains comments? wpf wpf

Is there a way to comment out XAML that contains comments?


No, there is no way of having nested comments in XAML.

You could use the mc:Ignorable attribute on your root element, and any attribute or element prefixed with that value will be ignored E.g:

<UserControl ...   mc:Ignorable="i">   <!-- Ignore Text attribute -->   <TextBlock i:Text="Hello" />   <!-- Ignore entire button -->   <i:Button>   </i:Button></UserControl>

Note that blend sets the mc:Ignorable attributes value to 'd', so you'll need to use e.g. mc:Ignorable="d i"


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 regularexpression" 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.


Select the comment blockHit cntrl-K, control-c (The same shortcut as on the C# side for commenting out a block of code).The designer will shift your comment markers to comment the entire block.

cntrol-k, cntrol-u (Kode Uncomment) will unshift things back to making it live XAML code again. This removes all the comment markings, so you make have to re-comment your original comments again.

its not perfect, but they are easy shortcuts you probably already know.