XAML TextBlock

This article shows how to use XAML TextBlock element.
  • 3539

TextBlock

The <TextBlock /> element of XAML write text. It usually comes after TextFlow. A TextBlock has many common properties such as FontFamily, Background, Foreground and others.

The following code uses TextBlock to write "Hello Text".

<TextFlow FontFamily="Verdana" Background="LightYellow" Foreground="DarkBlue" FlowDirection="LeftToRightThenTopToBottom" >
    <TextBlock FontSize="18" Foreground="Black"><Bold><Underline>Hello Text </Underline></Bold></TextBlock>
</
TextFlow>

The output looks like Figure 1.

XamlTextImg1.gif

Figure 1. Generating text using TextBlock

Complete Example

Listing 1 shows a complete example uses above discussed XAML elements.

<Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" Width="400" Height="300" 
>

    <TextFlow FontFamily="Verdana" Background="LightYellow" Foreground="DarkBlue" >
        <TextBlock FontSize="18" Foreground="Black"><Bold><Underline>Hello Text </Underline></Bold></TextBlock>
        <LineBreak/>
        <Paragraph>
            Hello Longhorn Corner. This sample shows you how to write text in XAML. Some of the XAML tags you
will see in this example are TextFlow, Paragraph, TextBlok and LineBreak.
       
</Paragraph>
        <LineBreak/>

        <TextBlock FontSize="20" Foreground="Blue">Text Block II</TextBlock>
        <Block>The Block element is used to create a new text block as you can see from this example.</Block>
        <Block FontFamily="Times New Roman" FontSize="16" Foreground="Red">
            Here is another block with different font and color.
        
</Block>
        <LineBreak/>
        <PageBreak/>
        <Block Foreground="Green">This is a block after page break.</Block>
        <Paragraph>This is a paragraph after page break. This is a paragraph after page break.
This is a paragraph after page break. This is a paragraph after page break.
This is a paragraph after page break. This is a paragraph after page break.
This is a paragraph after page break. This is a paragraph after page break.
       
</Paragraph>

    </TextFlow>
</
Grid>

The output of Listing 1 generates Figure 2.

XamlTextImg6.gif

Figure 2. XAML Text elements in action
 

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.