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
To download this code sample, click the following link:
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.
Include the custom font file in the Silverlight project.
Note In this sample, the custom font file is Magnetob.ttf.
Select the custom font file in the Solution Explorer window of Microsoft Visual Studio.
In the Properties window, set Build Action to Resource.
Set FontFamily in XAML by using the following syntax:
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.
In Expression Blend, click the Font Manager button that is on the Text tab in the Properties panel.
Select the font(s) that you want to embed in the Silverlight application.
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.
This code sample contains the following programming languages.
This sample application was created by using Visual Studio 2008 with Service Pack 1 installed.
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:
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.