Quick tip: Commenting out properties in XAML

Often when you write XAML, you wish you could ignore a property temporarily. In code, it is easy to do: Just comment out the line where the property is set, and you are good to compile.

LayoutRoot.Background = new SolidColorBrush(Colors.Red);
//LayoutRoot.DummyProperty = "Ignored";
/* LayoutRoot.Another = "Ignored too"; */

In XAML it is not so easy, because XML (of which XAML is a dialect) does not have line comments, but only block comments. You can comment out a whole XAML element, but not just one property.

<!--<ThisBlockIsIgnored Hello="World"
                    Again="Blah">
    <Label Content="No parse" />
</ThisBlockIsIgnored>-->
<TextBlock Text="This is parsed"
   Tag="This too"
   <!--DummyAttribute="No parse"-->
   Margin="10"/>
This last blocks creates an error.

Commenting single properties in XAML requires just a little more initial work, but then it is very easy:

  • In the root tag, add a new xmlns statement to import the Open XML markup compatibility elements:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  • Then, add another xmlns statement mapping a prefix of your choice (I like to use “ignore”) to an URI of your choice (for example “http://www.galasoft.ch/ignore”). Note that this URI does not need to point to anything on the web. This is just a unique resource identifier, something like a unique ID.
xmlns:ignore="http://www.galasoft.ch/ignore"
  • Finally, use the mc:Ignorable property to set the new prefix as ignorable.
mc:Ignorable="ignore"
  • Note: If you already had one ignorable prefix defined (for example the “d” prefix that Expression Blend and the Visual Studio designer use), no problems. Just add the new prefix to the Ignorable list with a space to separate the prefixes.
mc:Ignorable="d ignore"

The XAML parser honors the Ignorable property and will simply ignore any value prefixed by one of the prefixes defined in the list. Do not however use the Blend “d” ignorable prefix, because this has a special meaning for Blend and Visual Studio designer. The way described here defines a brand new prefix without any additional meaning. The “ignore” prefix can be used for properties or for whole blocks (including their content):

Single property:

<TextBlock Text="This is parsed"
   Tag="This too"
   ignore:DummyAttribute="No parse"
   Margin="10"/>

Whole block:

<ignore:ThisBlockIsIgnored Hello="World"
                    Again="Blah">
    <Label Content="No parse" />
</ignore:ThisBlockIsIgnored>  

Print | posted on Monday, February 1, 2010 5:26 AM

Feedback

# re: Quick tip: Commenting out properties in XAML

left by Jose Luis Latorre at 2/1/2010 10:59 AM Gravatar
Nice trick! cool!

# re: Quick tip: Commenting out properties in XAML

left by Raghuraman at 2/1/2010 11:02 AM Gravatar
Neat.

Thx for this tip.

# re: Quick tip: Commenting out properties in XAML

left by Graeme Hill at 2/1/2010 2:11 PM Gravatar
Definitely a very useful trick, but I have found that you need to be careful with this. Sometimes I forget that a property has been ignored and I can't find out why the UI is behaving differently. Usually when you look at code it is really obvious that something has been commented out because the syntax highlighter will give it a different colour, but in this case it just looks like regular code.

In many cases it's still way better than just deleting the property though.

# re: Quick tip: Commenting out properties in XAML

left by shemesh at 2/3/2010 7:18 PM Gravatar
NICENESS !

# re: Quick tip: Commenting out properties in XAML

left by Viktor Larsson at 2/17/2010 2:16 AM Gravatar
Nice although a workaround. We really should be able to comment in the middle of code. Still useful though :)

# re: Quick tip: Commenting out properties in XAML

left by Michael Brown at 5/12/2011 2:50 PM Gravatar
What if you want to ignore an attribute that already has a prefix?

# re: Quick tip: Commenting out properties in XAML

left by Cory Plotts at 5/13/2011 3:17 PM Gravatar
Love it ... know if it only highlighted the ignored sections green ...

# re: Quick tip: Commenting out properties in XAML

left by Cory Plotts at 5/13/2011 3:19 PM Gravatar
I mean ... _Now_ if it only highlighted the ignored sections green ...

# re: Quick tip: Commenting out properties in XAML

left by Laurent Bugnion at 5/16/2011 12:01 PM Gravatar
@Mike: I don't have a solution for that.
@Cory: I agree, oh yes I agree...
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: