tmoltzan
tmoltzan

Reputation: 371

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

I am fairly new to WPF and using XAML. I get really frustrated that I can not select a chunk of XAML and comment it out in Visual Studio 2010 using the comment button in the toolbar if the highlighted section already includes some comments.

Other languages allow you to nest comments inside of comments with no issue. Is there a way to comment out a comment in XAML using Visual Studio 2010?

Upvotes: 31

Views: 39935

Answers (4)

devdigital
devdigital

Reputation: 34349

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"

Upvotes: 30

Clint StLaurent
Clint StLaurent

Reputation: 1268

Select the comment block Hit 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.

Upvotes: 1

Tijo Tom
Tijo Tom

Reputation: 517

There is no commenting/uncommenting button in expression blend, if you want to try to comment your code block, you can type include the below symbols in the begin and the end of the code block manually

<!-- your code comes here..
Next Line--> 

See a sample screenshot below

See a sample screenshot below

Upvotes: 0

Louis
Louis

Reputation: 725

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.

Upvotes: 5

Related Questions