TextBlock Control in Silverlight

TextBlock control

TextBlock control allows you to apply effects to your text such as strike-through, shadows and borders. The TextBlock control are used to display the text. This control allows you to render read only text.

Properties - These are the following properties of this control.

  1. The Width property of the TextBlock element represent the width of a TextBlock.
  2. The Height property of the TextBlock element represent the width and the height of a TextBlock.
  3. The Text property of the TextBlock element represents the content of a TextBlock.
  4. The Foreground property sets the foreground color of contents or text.
  5. Margin Gets or sets the outer margin of a TextBlock control.
  6. MaxHeight Gets or sets the maximum height constraint of a TextBlock control.
  7. MaxWidth Gets or sets the maximum width constraint of a TextBlock control.
  8. Opacity Gets or sets the degree of the object's opacity.

Creating a TextBlock

Creating a TextBlock control in XAML.

<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TextBlock1" Text="Hello, Rohatash" VerticalAlignment="Top" />

There are also a variety of options you can apply to your font.

FontFamily - The font typeface family name.

<TextBlock FontFamily="Courier New">Hello, Rohatash</TextBlock>
TextBlock FontFamily="Times New Roman" Canvas.Top="10">Hello, Rohatash</TextBlock>
<TextBlock FontFamily="Verdana" Canvas.Top="20">Hello, Rohatash</TextBlock>

FontSize - The font size is the size in pixels.

<TextBlock FontSize="10">Hello, Rohatash</TextBlock>
TextBlock FontSize="20" Canvas.Top="20">Hello, Rohatash</TextBlock>
<TextBlock FontSize="30" Canvas.Top="50">Hello, Rohatash</TextBlock>

FontWeight - FontWeight has the following option. such as Thin, ExtraLight, Light, Normal, Medium, SemiBold, Bold, ExtraBold, Black, ExtraBlack.

<TextBlock Text="Hello, Rohatash" FontWeight="Normal"  Canvas.Top="0"></TextBlock>

<TextBlock Text="Hello, Rohatash" FontWeight="Bold"  Canvas.Top="10"></TextBlock>

Foreground - This allows you to change the color of the font.


<TextBlock Text="Hello, Rohatash" FontSize="30" FontFamily="Arial" Canvas.Top="0">  <TextBlock.Foreground>


        <GradientStop Color="#FF0000FF" Offset="0.0" />

        <GradientStop Color="#FFEEEEEE" Offset="1.0" />




        <TextBlock Text="Hello, Rohatash" FontSize="30" FontFamily="Arial" Canvas.Top="20" Foreground="Red" Margin="0,34,0,-34"></TextBlock>


TextDecorations - A text decoration is a visual ornament that you can add to text.

<TextBlock TextDecorations="underline"  TextWrapping="Wrap">Hello, Rohatash</TextBlock>

Runs - we can inter-mix different runs of text formatting within the same block using the Run element. For example, you can display a string like this:

<TextBlock Margin="0,12,0,-12">

    <Run FontWeight="Bold">Hello, Rohatash</Run>

    <Run Foreground="Red">How are you?</Run>

    <Run FontStyle="Italic">I am fine thanks!</Run>