Escaping Curly Braces in XAML

Skill

Escaping Curly Braces in XAML

Posted in:

You may have already come across this dilemma - needing to put curly braces in the content of a XAML object - like a TextBlock. In XAML, { and } are used to denote markup extensions, so they can't be used without escaping them. Unfortunately, the escape mechanism is far from obvious.

In order to put curly braces in XAML, you must precede the literal with an empty set of braces - {}.

<!-- This works fine -->
<TextBlock Text="{}This is {my} text!" />

<!-- This will result in a compile error -->
<TextBlock Text="This is {my} text!" />

MSDN has posted an article explaining this escape sequence in detail, so check there for more information.

Sanket Vasa
10/28/2008 - 06:26

Not the most intuitive way to escape special characters...
Btw, shouldn't it be "precede" instead of "proceed" :)

reply

The Fattest
10/28/2008 - 07:16

Sanket, thanks for the comment the grammar mishap has been fixed. :)

reply

russ
09/08/2010 - 01:10

I'm generating XAML that used in Silverlight 1 and WPF. (strange combination I know).

Silverlight 1 displays the {} - presumably because it doesn't have any databinding, so just displays curly braces as is. So I ended up replacing the braces with their character entities { and &#125.

reply

The Reddest
09/08/2010 - 08:18

Good idea russ. I often forget that XAML supports that. I've used a similar idea for adding line breaks to TextBlocks.

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.