Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #58 – Text Wrapping and Line Breaks in TextBlocks

TextBlocks are a great control to use in Silverlight to display read only text. In Tip of the Day #45 I covered the basic usage of TextBlocks including formatting and text runs.

For this tip I would like to show you how to make a TextBlock with wrapping text and line breaks.

To make a TextBlock wrap its text you will need to set TextWrapping ="Wrap" on the TextBlock. The following example below shows a TextBlock in a ContentControl that is only 100 pixels wide. With TextWrapping set to “Wrap”, the TextBlock will wrap to a new line each time its contents reach 100 pixels in width.

XAML:

<Canvas Margin="10">
    <ContentControl Width="100">
        <TextBlock TextWrapping="Wrap">        

            A lie can travel half way around the world while the truth is putting on its shoes.        

            --Mark Twain
        </TextBlock>
    </ContentControl>
</Canvas>

Result with TextWrapping = “Wrap”:

image

Without TextWrapping:

image

As you may notice the text above does not include any of the line breaks seen in the XAML. To insert a line break all you need to do is insert a <LineBreak> tag within the text.

Example:

<Canvas Margin="10">
    <ContentControl Width="100">
        <TextBlock TextWrapping="Wrap">
            A lie can travel half way around the world while the truth is putting on its shoes.
            <LineBreak></LineBreak>
            <LineBreak></LineBreak>
            --Mark Twain
        </TextBlock>
    </ContentControl>

</Canvas>

Result:

image 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

TextBlocks are a great control to use in Silverlight to display read only text. In Tip of the Day #45

# October 9, 2008 8:35 PM

Community Blogs said:

In this issue: Andrea Boschin, Jeff Handley, Mike Snow, Laurence Moroney, Terence Tsang, and Jesse Liberty

# October 10, 2008 12:42 AM

2008 October 10 - Links for today « My (almost) Daily Links said:

Pingback from  2008 October 10 - Links for today &laquo; My (almost) Daily Links

# October 10, 2008 4:05 AM

Silverlight news for October 10, 2008 said:

Pingback from  Silverlight news for October 10, 2008

# October 10, 2008 9:44 AM

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

# October 10, 2008 9:47 AM

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

# October 12, 2008 6:05 PM

Silverlight news for October 13, 2008 said:

Pingback from  Silverlight news for October 13, 2008

# October 13, 2008 7:55 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #66 Title: How to copy XAML for Silverlight from Expression Designer Silverlight

# November 3, 2008 1:41 PM

Toutes les astuces pour mieux utiliser Visual Studio said:

Les TextBlocks sont de bons contrôles utilisés pour l’affichage de textes en lecture seule en Silverlight.

# November 10, 2008 7:59 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM