wolfgang ziegler


„make stuff and blog about it“

How to escape curly braces in XAML?

April 17, 2013

xaml_logo

If you want to display curly braces in text elements of your XAML UI, you can easily run into problems because curly braces usually indicate markup extensions like data bindings or resources.

Let’s assume we want to display the text {dev news} in a WPF TextBlock. Our first attempt will probably look something like this:

<TextBlock Text="{dev news}"/>

Unfortunately, this causes a syntax error in the XAML parser because this notation indicates a markup extension.

The easy solution is to add an extra pair of leading curly braces acting as escape sequence:

<TextBlock Text="{}{dev news}" />

 

If you are working in older versions of .NET and WPF or Silverlight, this escape sequence functionality is probably not available, resulting in yet another XAML parser error.
In this case a good workaround is to keep the string containing the curly braces in a view model property and bind to it from XAML.

<TextBlock Text="{Binding Path=TextWithCurlyBraces}" />
public class MyViewModel
{
  public string TextWithCurlyBraces
  {
    get { return "{dev news}"; }
  }
}