Switch On The Code RSS Button - Click to Subscribe
Oct
27

Escaping Curly Braces in XAML

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.



Posted in XAML, All Tutorials by The Reddest |

2 Responses

  1. Sanket Vasa Says:

    Not the most intuitive way to escape special characters…
    Btw, shouldn’t it be “precede” instead of “proceed” :)

  2. The Fattest Says:

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

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash