Showing formatted text in a TextBlock in Windows Phone apps

Here is a quick tip on Windows Phone development. If you want to show formatted text i.e. text having multiple styles or colors in a single line in your windows phone apps then you can easily do so with a TextBlock. Example below shows how you can show a normal text along with a highlighted text and a bold text all in the same line in a TextBlock. Text Wrapping also comes for free as it is handled by TextBlock itself.

Here is the XAML:
<TextBlock TextWrapping=”Wrap” Margin=”12″>

<TextBlock.Inlines>

<Run FontWeight=”Light” Text=”Normal Text”/>

<Run Foreground=”Aqua” Text=” Highlighted Text”/>

<Run FontWeight=”Bold” Text=” Bold text”/>

<Run FontWeight=”Light” Text=” Normal Text”/>

</TextBlock.Inlines>

</TextBlock>

Here is the result:

Formatted Text in WindowsPhone App

In case you want databinding to work with the multiple formatted text runs then kindly see the following link: http://stackoverflow.com/questions/5253622/databinding-textblock-runs-in-silverlight-wp7

Leave a Reply