31

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?

tmoltzan
  • 371
  • 1
  • 3
  • 6
  • In the old C with `/**/`-style comments, as well as in old Pascal with `{}` (not sure about the modern dialects), nested comments were an issue. – Vlad Dec 17 '10 at 21:29
  • I was thinking more of using the comment/uncomment buttons in visual studio. You point is valid though. – tmoltzan Dec 17 '10 at 22:28
  • I don't have VS 2010, but in 2012 You can simply use HTML type comments. Try it. – Maz T Feb 06 '17 at 11:32

4 Answers4

30

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"

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • 6
    I don't actually do it that often, but sometimes when I am trying different ways of doing things in templates, I don't want to delete a working template to try a new approach. I am still poking around with templates quite a bit just seeing what I can do. – tmoltzan Dec 17 '10 at 22:34
  • 1
    This works fine except for ResourceDictionary files. They don't seem to have that property. – Dan Bailiff Sep 02 '11 at 22:01
  • 1
    *"I don't want to delete a working template to try a new approach"* - That sounds like you aren't using an [SCM](https://en.wikipedia.org/wiki/Software_configuration_management). Among many other things, an SCM allows you to play around, without breaking things. – IInspectable Jul 31 '16 at 10:01
  • 4
    Why is it hard to understand why someone would want to comment out xaml? Isn't it common to want to disable code without deleting it? – Kyle Delaney Apr 07 '17 at 22:49
  • The spec *should* have allowed for single-line comments like C, BASIC, etc. had for *DECADES* prior. Then, IDEs (like VS already does with C# even though C# has a multi-line comment feature), keep adding more single-line comment prefixes to each line even if multiple lines are marked for commenting. That's a no brainer. The possibility of resulting ML being in error is no excuse for not offering single-line comments. The same problem exists with any programming language that allows single-line (or even multi-line) comments. It's the coder's job to make sure the resulting code still works. – Tom Jul 24 '19 at 18:14
5

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.

Louis
  • 705
  • 10
  • 18
1

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.

Clint StLaurent
  • 1,238
  • 12
  • 11
0

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

Community
  • 1
  • 1
Tijo Tom
  • 487
  • 6
  • 13