Posted by: Zeeshan Amjad | April 15, 2010

Using special characters in XAML


I came across one question in MSDN forum about how to use the special characters such as <, >, & etc in WPF application or to be more specific in XAML. In fact there are multiple ways to solve this problem. One simplest approach is to use the C# code. But the main problem with C# code is that sometimes we even don’t have name of the controls such as when using MVVM. In that case we have to give names of all the controls unnecessary.

The other approach is using XAML. Even in XAML there are more than one way to do the same thing. First method is use the XML special characters, such as &amp; for “&”, &lt for “<”, &gt for “>” etc. Here is one example of this.

  1: <Window x:Class="WpfApplication.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="WPF Application" Height="300" Width="300">
  5:     <Grid>
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition Height="4*"/>
  8:             <RowDefinition/>
  9:         </Grid.RowDefinitions>
 10:         <Grid.ColumnDefinitions>
 11:             <ColumnDefinition/>
 12:             <ColumnDefinition/>
 13:             <ColumnDefinition/>
 14:         </Grid.ColumnDefinitions>
 15:         <TextBlock Margin="5" Grid.ColumnSpan="3">
 16:             Example to demonstrate special characters.
 17:         </TextBlock>
 18:         <Button Margin="5" Grid.Column="1" Grid.Row="1">&lt;&lt;</Button>
 19:         <Button Margin="5" Grid.Column="2" Grid.Row="1">&gt;&gt;</Button>
 20:     </Grid>
 21: </Window>
 22: 

The other approach is to use CDATA. Here is a same example of using CDATA.

  1: <Window x:Class="WpfApplication.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="WPF Application" Height="300" Width="300">
  5:     <Grid>
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition Height="4*"/>
  8:             <RowDefinition/>
  9:         </Grid.RowDefinitions>
 10:         <Grid.ColumnDefinitions>
 11:             <ColumnDefinition/>
 12:             <ColumnDefinition/>
 13:             <ColumnDefinition/>
 14:         </Grid.ColumnDefinitions>
 15:         <TextBlock Margin="5" Grid.ColumnSpan="3">
 16:             Example to demonstrate special characters.
 17:         </TextBlock>
 18:         <Button Margin="5" Grid.Column="1" Grid.Row="1"><![CDATA[<<]]></Button>
 19:         <Button Margin="5" Grid.Column="2" Grid.Row="1"><![CDATA[>>]]></Button>
 20:     </Grid>
 21: </Window>
 22: 

Output of both program is same. Here is the output of the program.

WPFSpecialCharacter

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: