Using XAML TextBlock

XAML <TextBlock> element represents a TextBlock control.
 
The following code example is how a TextBox is declared in XAML.
  1. <TextBlock Name="TextBlock1" Height="30" Width="200"   
  2.     Text="Hello! I am a TextBlock." Foreground="Red">  
  3. </TextBlock>  
The following code example sets margin and alignment of the control.
  1. <TextBlock Name="TextBlock1" Height="30" Width="200"   
  2.         Text="Hello! I am a TextBlock."   
  3.         Margin="10,10,0,0" VerticalAlignment="Top"   
  4.         HorizontalAlignment="Left">              
  5. </TextBlock>  
                                                    
The following code example sets TextBlock's font including font name, font size, and font styles.
 
  1. FontSize="14" FontFamily="Verdana" FontWeight="Bold"  
                                                     
The TextWrapping property sets the wrap of no wrap text.  
  1. TextWrapping="Wrap"  
The TextAlignment property sets the text alignment of text in the control.
 
  1. TextAlignment="Right"  
The Padding property sets the space between a boundary and the text that can be applied to all sides or a selected side of the boundary. The padding spacing is based on left, right, top and bottom.
  1. <TextBlock Name="TextBlock1" Height="30" Width="200"   
  2.     Text="Hello! I am a TextBlock." Foreground="Red"  
  3.     Margin="10,10,0,0" VerticalAlignment="Top"   
  4.     HorizontalAlignment="Left"  
  5.     FontSize="14" FontFamily="Verdana" FontWeight="Bold"  
  6.     TextWrapping="Wrap" TextAlignment="Center" Padding="2">  
  7. </TextBlock>  
The Inlines property represents the collection of inline text within the control.
 
  1.     <Run FontWeight="Bold" FontSize="14" Text="Hi! I am a TextBlock. " />   
  2.     <Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />  
  3.     <Run FontStyle="Italic" FontSize="18" Text="Here is some linear gradient text. ">  
  4.         <Run.Foreground>  
  5.             <LinearGradientBrush>   
  6.                 <GradientStop Color="Green" Offset="0.0" />   
  7.                 <GradientStop Color="Purple" Offset="0.25" />   
  8.                 <GradientStop Color="Orange" Offset="0.5" />   
  9.                 <GradientStop Color="Blue" Offset="0.75" />   
  10.               </LinearGradientBrush>   
  11.         </Run.Foreground>  
  12.     </Run>              
  13.     <Run FontStyle="Italic" Foreground="Green" Text="How about adding some green? " />              
  14. </TextBlock.Inlines>  
For a detailed tutorial on WPF TextBlock, please visit Working with TextBlock in WPF.


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.