Export (0) Print
Expand All

LineBreak Object

Represents an explicit new line in a TextBlock.

XAML
<LineBreak .../>
Scripting
To create an object using scripting, see CreateFromXAML.

Remarks

Properties (or attributes in XAML) set on a LineBreak are generally ignored by rendering. For instance, setting the FontSize of a LineBreak has no effect on the vertical space between the previous and next lines of text; only the FontSize set on the text runs themselves has any effect. The line height for a LineBreak is influenced by the surrounding runs, but multiple line breaks in succession will use the default 14.66 pixel height.

LineBreak explicitly does not support the Text property. Attempting to set Text in XAML or placing any inner text in a LineBreak will result in a parsing error (use the containerless tag form <LineBreak /> to avoid any problems). Attempting to get or set it in script will result in a run time error. This is potentially an issue if you are iterating the Inlines collection and attempting to access all the Text values. To work around this you might have to use try-catch logic, or check the ToString value before attempting to get a Text value.

Examples

The following XAML example shows how to use LineBreak objects to force the formatted text strings defined in the Run objects to display on separate lines.

XAML
<!-- Display formatted text as Run objects within a TextBlock. -->
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007">

<TextBlock
  FontFamily="Arial" Width="400" Text="Sample text formatting runs">
  <LineBreak/>
  <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
  <LineBreak/>
  <Run Foreground="Teal" FontFamily="Times New Roman" FontSize="18" FontStyle="Italic">Times New Roman Italic 18</Run>
  <LineBreak/>
  <Run Foreground="SteelBlue" FontFamily="Verdana" FontSize="14" FontWeight="Bold">Verdana Bold 14</Run>
</TextBlock>

</Canvas>

The following illustration shows the rendered formatted text from the previous XAML content example.

Rendered formatted text

Rendered formatted text

Without the LineBreak object, the text in each Run would flow together as one line, and eventually be truncated after exceeding the TextBlock's Width value. The following illustration shows how the formatted text would render without using LineBreak objects.

Rendered formatted text without line breaks

Rendered formatted text without line breaks

See Also

Text and Fonts Overview
TextBlock
Run

Community Additions

Show:
© 2014 Microsoft