IdentityMine

| Tags: Laurent Bugnion, Silverlight, WPF

originally posted by Laurent Bugnion: (link) - please comment at original post

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>

Remember to please comment at original post: (link)

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on TumblrEmail this to someoneDigg thisFlattr the authorShare on StumbleUpon

Comments are closed.