Escaping {} in XAML

02 October, 2014 · less than a minute to read · written by Craig Pickles

An escape character gives an alternative meaning to subsequent characters, In XAML the use of an open brace ‘{‘ denotes the start of a markup extension. While parsing the XAML, the reader finds the content inside the curly braces and creates an instance of it to use in the output.

For example:

<TextBlock text="Hello {username}">

Will output “Hello username” with no braces around username as an instance of the string username is returned (minus the braces).

To solve this problem we need to use escape sequences.

When an open brace ‘{‘ is detected by a XAML reader the reader will first check the next character to determine whether it is a closing brace ‘}’. If the two braces ‘{}’ are adjacent they are considered an escape sequence. If an escape sequence is encountered, the XAML reader will process the rest of the string as a string.

For example:

<TextBlock text="{}Hello {username}">

Will output “Hello {username}”