Using Label to add other controls in text
The Label
control is another way of representing text in WPF application. It looks like what TextBlock
control offers, but instead of having only text support, it can also host any kind of other controls. It exposes the Content
property to host text and other controls.
In this recipe, we will explore how to use the Label
control in a WPF.
Getting ready
To get started with this control, open Visual Studio to create an application based on the WPF project template and call it CH02.LabelDemo
.
How to do it...
Once the project gets created, follow these simple steps to add text in your application UI, using the Label
control:
- Open the
MainWindow.xaml
file to change the application UI. - Replace the existing
Grid
panel with the following XAML code:
<StackPanel Margin="10 10 10 20"> <Label Content="1. This is a Label control" /> <Label Content="2. A Label control with text formatting" FontWeight="Bold" Foreground="Red" ...