Partilhar via


Quickstart: displaying text (XAML)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

The XAML framework provides several controls for rendering text, and a set of properties for formatting the text. The controls for displaying read-only text are TextBlock and RichTextBlock. This quickstart shows you how to use TextBlock controls to display text.

Roadmap: How does this topic relate to others? See:

Prerequisites

We assume that you can create a basic Windows Runtime app using C++, C#, or Visual Basic. For instructions on adding a control, see Quickstart: Adding controls and handling events.

TextBlock

TextBlock is the primary control for displaying read-only text in Windows Runtime apps using C++, C#, or Visual Basic. You can display text in a TextBlock control using its Text property. This XAML shows how to define a TextBlock control and set its Text property to a string.

<TextBlock Text="Hello, world!" />

You can also display a series of strings in a TextBlock, where each string has different formatting. You can do this by using a Run element to display each string with its formatting and by separating each Run element with a LineBreak element.

Here's how to define several differently formatted text strings in a TextBlock by using Run objects separated with a LineBreak.

<TextBlock FontFamily="Arial" Width="400" Text="Sample text formatting runs">
    <LineBreak/>
    <Run Foreground="LightGray" FontFamily="Courier New" FontSize="24"> 
        Courier New 24 
    </Run>
    <LineBreak/>
    <Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18" FontStyle="Italic"> 
        Times New Roman Italic 18 
    </Run>
    <LineBreak/>
    <Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14" FontWeight="Bold"> 
        Verdana Bold 14 
    </Run>
</TextBlock>

Here's the result.

Summary and next steps

You learned how to create TextBlock controls to display text in your app.

For more code examples that show these controls, see the XAML text display sample.

Roadmap for Windows Runtime apps using C# or Visual Basic

Roadmap for Windows Runtime apps using C++