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

Tagged with:
Kanishk Kunal

Kanishk is a professional developer who builds apps and websites for both fun & profit. After working at Microsoft for more than 6 years, he now works independently and spends most of his time punching his keyboard and swiping his smartphone. Follow him on twitter @kanishkkunal

No comment yet, add your voice below!

Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name
Email *
Website