views:

115

answers:

4

In XAML, if you insert

<TextBlock Text="Hello World" />

You will see the words "Hello World".

If you insert

<TextBlock Text="{Binding}" />

it will trigger the data binding functionality. But what if I really wanted the display text to be "{Binding}"?"

Are there the equivalent of escape characters in XAML strings?

Or is my only solution to do this:

<TextBlock>Binding</TextBlock>

Just curious.

+8  A: 

You can escape the entire string with "{}":

<TextBlock Text="{}{Binding}"/>

Or individual curly braces can be escaped with a backslash:

<TextBlock Text="{Binding Foo,StringFormat='Hello \{0\}'}" />
Matt Hamilton
+1  A: 

Try this:

<TextBlock Text="&#123;Binding&#125;" />

And unescape it when you read the value.

Traveling Tech Guy
Oops, voted this up too soon. It works in the IDE display, but then I get a compile error. "The tag 'Binding;' does not exist in XML namespace 'schemas.microsoft.com/winfx/2006/…'. "
Andrew Shepherd
Come to think of it, even I think escaping with a '\' makes more sense. Oh well, win some - lose some :)
Traveling Tech Guy
A: 

You need to escape the { and } characters, so you'd end up with <TextBlock Text="\{Binding\}" />

Pete OHanlon
That doesn't work. I tried it, and I saw the '\' characters in the display.
Andrew Shepherd
+4  A: 

Escaping with '{}' as per Matt's response is the way to go, but for the sake of completeness you can also use a CDATA section:

<TextBlock>
    <TextBlock.Text>
        <![CDATA[{Binding}]]>
    </TextBlock.Text>
</TextBlock>

A CDATA section is more useful for multiline text though.

HTH, Kent

Kent Boogaart