Search code examples
xamlattributescomments

Why can't I comment attributes in XAML?


This has been bothering me for a while, maybe I am missing something.

The following throws an error with the commented attribute (expected >), but shouldn't I be able to do something like this?

<Label x:Name="Gaga"
               FontSize="20"
               <!--
               Content="{Binding SomethingThatIsEmptyAtDesignTime"}
                -->
               Content="LookAtMe!"
               />

Solution

  • Though you can't comment out using the basic XAML markup, you can achieve the desired results by importing the Open XML markup namespace.

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ignore="http://www.galasoft.ch/ignore"
    mc:Ignorable="ignore"
    
    <Label x:Name="Gaga"
           FontSize="20"
           ignore:Content="{Binding SomethingThatIsEmptyAtDesignTime}"
           Content="LookAtMe!"
    />
    

    This blog post describes how to do it.