How to work with text in Silverlight 3

This article has been archived. It is offered "as is" and will no longer be updated.
Text plays a critical role in many applications. Microsoft Silverlight 3 provides extensive support for working with texts. This article describes an All-In-One Code Framework sample that is available for downloading. This sample includes a step-by-step guide about how to work with text in Silverlight 3.

This article covers the following functionalities:
  • Create a simple TextBlock
  • Create a TextBlock with multiple Runs and LineBreaks
  • Create a TextBlock with advanced format
  • Select all text in a TextBox using code

Difficulty level

Download information

To download this code sample, click the following link:


Technical overview

To customize the way text is displayed in Silverlight, you can use various methods. For example, to use custom font, you can use one of the following methods.

Method 1
  1. Include the custom font file in the Silverlight project.

    Note In this sample, the custom font file is Magnetob.ttf.
  2. Select the custom font file in the Solution Explorer window of Microsoft Visual Studio.
  3. In the Properties window, set Build Action to Resource.
  4. Set FontFamily in XAML by using the following syntax:
    <TextBlock FontFamily="Magnetob.ttf#Magneto"…
    Or, set FontFamily at code behind, as it is set in the sample:
    paragraph1.FontFamily = new FontFamily("Magnetob.ttf#Magneto");
    Note In this sample code, Magnetob.ttf is the font file name. The file name is case insensitive. Additionally, because one font file may contain multiple fonts, you must specify the fonts that you want to use. In this sample code, the Magneto font was specified.
Method 2
  1. In Expression Blend, click the Font Manager button that is on the Text tab in the Properties panel.
  2. Select the font(s) that you want to embed in the Silverlight application.
  3. Select your text control, and then choose the desired font for the text control.
Font management is just one of the text features that are supported by Silverlight 3. For more information about how to work with text in Silverlight 3, run the sample project. The sample itself is an interactive document.

Note For more information about how to create and then deploy the sample application, refer to the Readme.txt file that is included in the download package.

Technology category

  • Silverlight


This code sample contains the following programming languages.
LanguageProject Name
Visual C#CSSL3Text
Visual Basic .NETVBSL3Text



  • Text, Silverlight

What is All-In-One Code Framework?

All-In-One Code Framework uses code samples in different programming languages to illustrate most Microsoft development techniques. Each code sample is carefully selected, composed, and documented to show common code scenarios. For more information about All-In-One Code Framework, visit the following All-In-One Code Framework Web site:

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft Web site:
For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Rapid publishing disclaimer
Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

Article ID: 981904 - Last Review: 12/12/2015 03:59:19 - Revision: 1.1

Microsoft Silverlight for Windows, Microsoft Visual Studio 2008 Service Pack 1, Microsoft Visual Studio 2008 Academic Edition, Microsoft Visual Studio 2008 Professional Edition, Microsoft Visual Studio 2008 Standard Edition, Microsoft Visual Studio Team System 2008 Database Edition, Microsoft Visual Studio Team System 2008 Architecture Edition, Microsoft Visual Studio Team System 2008 Development Edition, Microsoft Visual Studio Team System 2008 Test Edition, Microsoft Visual Studio Team System 2008 Team Suite

  • kbnosurvey kbarchive kbexpertiseadvanced kbsurveynew kbinfo kbnomt kbrapidpub kbcodefx atdownload KB981904