- WPF - Home
- WPF - Overview
- WPF - Environment Setup
- WPF - Hello World
- WPF - XAML Overview
- WPF - Elements Tree
- WPF - Dependency Properties
- WPF - Routed Events
- WPF - Controls
- WPF - Layouts
- WPF - Nesting Of Layout
- WPF - Input
- WPF - Command Line
- WPF - Data Binding
- WPF - Resources
- WPF - Templates
- WPF - Styles
- WPF - Triggers
- WPF - Debugging
- WPF - Custom Controls
- WPF - Exception Handling
- WPF - Localization
- WPF - Interaction
- WPF - 2D Graphics
- WPF - 3D Graphics
- WPF - Multimedia
WPF - Textblock
A TextBlock is a lightweight control for displaying small amounts of read-only text. The hierarchical inheritance of TextBlock class is as follows −
Commonly Used Properties of TextBlock Class
| Sr. No. | Property & Description |
|---|---|
| 1 |
ContentEnd Gets a TextPointer object for the end of text content in the TextBlock. |
| 2 |
ContentStart Gets a TextPointer object for the start of text content in the TextBlock. |
| 3 |
IsTextSelectionEnabled Gets or sets a value that indicates whether text selection is enabled in the TextBlock, either through user action or calling selection-related API. |
| 4 |
IsTextSelectionEnabledProperty Identifies the IsTextSelectionEnabled dependency property. |
| 5 |
LineHeight Gets or sets the height of each line of content. |
| 6 |
MaxLines Gets or sets the maximum lines of text shown in the TextBlock. |
| 7 |
SelectedText Gets a text range of selected text. |
| 8 |
SelectionEnd Gets the end position of the text selected in the TextBlock. |
| 9 |
SelectionHighlightColor Gets or sets the brush used to highlight the selected text. |
| 10 |
SelectionStart Gets the starting position of the text selected in the TextBlock. |
| 11 |
Text Gets or sets the text contents of a TextBlock. |
| 12 |
TextAlignment Gets or sets a value that indicates the horizontal alignment of text content. |
| 13 |
TextTrimming Gets or sets the text trimming behavior to employ when content overflows the content area. |
| 14 |
TextWrapping Gets or sets how the TextBlock wraps text. |
Commonly Used Events of TextBlock Class
| Sr. No. | Event & Description |
|---|---|
| 1 |
ContextMenuOpening Occurs when the system processes an interaction that displays a context menu. |
| 2 |
SelectionChanged Occurs when the text selection has changed. |
Commonly Used Methods in TextBlock Class
| Sr. No. | Method & Description |
|---|---|
| 1 |
Focus Focuses the TextBlock, as if it were a conventionally focusable control. |
| 2 |
Select Selects a range of text in the TextBlock. |
| 3 |
SelectAll Selects the entire contents in the TextBlock. |
Example
- Lets create a new WPF project with WPFTextBlockControl.
- Drag a text block from the toolbox.
- Change the background color of the text block from the properties window.
- The following example shows the usage of TextBlock in an XAML application.
- Here is the XAML code in which a TextBlock is created with some properties.
<Window x:Class = "WPFTextBlockControl.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WPFTextBlockControl"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<TextBlock FontFamily = "Verdana"
LineStackingStrategy = "MaxHeight" LineHeight = "10" Width = "500"
TextWrapping = "Wrap" Background = "#FFE2B1B1" Margin = "48,8,48,10">
Use the <Run FontSize = "30">LineStackingStrategy</Run> property to determine how
a line box is created for each line. A value of <Run FontSize = "20">MaxHeight</Run>
specifies that the stack height is the smallest value that contains all the inline
elements on that line when those elements are properly aligned. A value of <Run
FontSize = "20"> BlockLineHeight</Run> specifies that the stack height is
determined by the block element LineHeight property value.
</TextBlock>
</Grid>
</Window>
When you compile and execute the above code, it will produce the following output −
We recommend that you execute the above example code and try the other properties and events of TextBlock class.