Posted by: Zeeshan Amjad | October 14, 2009

Escape Sequence in XAML


We already studied how to use XAML markup extension. We used it several time while doing data binding. We are using {} for markup extension. Suppose we want to display these characters in text block (such as display the example of data binding in text block). Our first attempt would be something like this.

  1: <Window x:Class="xaml5.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="XAML" Height="300" Width="300" Background="AliceBlue">
  5:     <Grid>
  6:         <TextBlock Text="{test}" Margin="10"/>
  7:     </Grid>
  8: </Window>
  9: 

This will give compilation error. To escape {} we have to include one extra pair of curly braces. This is a proper way to display “{test}” in text block.

  1: <Window x:Class="xaml5.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="XAML" Height="300" Width="300" Background="AliceBlue">
  5:     <Grid>
  6:         <TextBlock Text="{}{test}" Margin="10"/>
  7:     </Grid>
  8: </Window>
  9: 

This would be the output of the following program.

XAML_Escape

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: