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 −
ContentEnd
Gets a TextPointer object for the end of text content in the TextBlock.
ContentStart
Gets a TextPointer object for the start of text content in the TextBlock.
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.
IsTextSelectionEnabledProperty
Identifies the IsTextSelectionEnabled dependency property.
LineHeight
Gets or sets the height of each line of content.
MaxLines
Gets or sets the maximum lines of text shown in the TextBlock.
SelectedText
Gets a text range of selected text.
SelectionEnd
Gets the end position of the text selected in the TextBlock.
SelectionHighlightColor
Gets or sets the brush used to highlight the selected text.
SelectionStart
Gets the starting position of the text selected in the TextBlock.
Text
Gets or sets the text contents of a TextBlock.
TextAlignment
Gets or sets a value that indicates the horizontal alignment of text content.
TextTrimming
Gets or sets the text trimming behavior to employ when content overflows the content area.
TextWrapping
Gets or sets how the TextBlock wraps text.
ContextMenuOpening
Occurs when the system processes an interaction that displays a context menu. 
SelectionChanged
Occurs when the text selection has changed.
Focus
Focuses the TextBlock, as if it were a conventionally focusable control.
Select
Selects a range of text in the TextBlock.
SelectAll
Selects the entire contents in the TextBlock.
Let’s 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.