Using the TextBlock control to add plain text
The TextBlock
control in WPF is a lightweight UI element, which is used to display text content to the screen. Almost everywhere, you will use this element in your application UI to display plain text in a single line or a multiline format. To add simple plain text, you can either write <TextBlock Text="Text message" />
or <TextBlock>Text message</TextBlock>
in your XAML page.
In this recipe, we will explore more about this UI element.
Getting ready
To get started, open your Visual Studio IDE, and create a new WPF project called CH02.TextBlockDemo
.
How to do it...
Now open the MainWindow.xaml
, and follow these steps to add TextBlock
control with various formatting options:
- First, change the pre-existing
Grid
panel to aStackPanel
. - Now add the following two
TextBlock
controls to it, which will have plain text in them:
<TextBlock Text="1. This is a TextBlock control, with 'Text' property" Margin="10 5" /> <TextBlock Margin...