XAML <TextBlock> element represents a TextBlock control.
The following code example is how a TextBox is declared in XAML.
- <TextBlock Name="TextBlock1" Height="30" Width="200"
- Text="Hello! I am a TextBlock." Foreground="Red">
- </TextBlock>
The following code example sets margin and alignment of the control.
- <TextBlock Name="TextBlock1" Height="30" Width="200"
- Text="Hello! I am a TextBlock."
- Margin="10,10,0,0" VerticalAlignment="Top"
- HorizontalAlignment="Left">
- </TextBlock>
The following code example sets TextBlock's font including font name, font size, and font styles.
- FontSize="14" FontFamily="Verdana" FontWeight="Bold"
The TextWrapping property sets the wrap of no wrap text.
The TextAlignment property sets the text alignment of text in the control.
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.
- <TextBlock Name="TextBlock1" Height="30" Width="200"
- Text="Hello! I am a TextBlock." Foreground="Red"
- Margin="10,10,0,0" VerticalAlignment="Top"
- HorizontalAlignment="Left"
- FontSize="14" FontFamily="Verdana" FontWeight="Bold"
- TextWrapping="Wrap" TextAlignment="Center" Padding="2">
- </TextBlock>
The Inlines property represents the collection of inline text within the control. - <Run FontWeight="Bold" FontSize="14" Text="Hi! I am a TextBlock. " />
- <Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
- <Run FontStyle="Italic" FontSize="18" Text="Here is some linear gradient text. ">
- <Run.Foreground>
- <LinearGradientBrush>
- <GradientStop Color="Green" Offset="0.0" />
- <GradientStop Color="Purple" Offset="0.25" />
- <GradientStop Color="Orange" Offset="0.5" />
- <GradientStop Color="Blue" Offset="0.75" />
- </LinearGradientBrush>
- </Run.Foreground>
- </Run>
- <Run FontStyle="Italic" Foreground="Green" Text="How about adding some green? " />
- </TextBlock.Inlines>
For a detailed tutorial on WPF TextBlock, please visit Working with TextBlock in WPF.