Bagikan melalui


LineBreak

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents an explicit new line in a TextBlock object.

<LineBreak   .../>

Managed Equivalent

LineBreak

Remarks

Properties (or attributes in XAML) set on a LineBreak object are generally ignored by rendering. For example, if you set the FontSize property of a LineBreak, it will have 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. If you attempt to set Text in XAML or place any inner text in a LineBreak, a parsing error will be raised. To avoid any problems, use the containerless tag form <LineBreak />. If you attempt to get or set LineBreak.Text in script, a run-time error will be raised. This is potentially an issue if you are iterating the Inlines (TextBlock) collection and attempting to access all the Text (TextElement) 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.

Example

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

<!-- Display formatted text as Run objects in a TextBlock. -->
<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation">

<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 example.

Rendered formatted text

Sample of text formatting runs.

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

Rendered formatted text without line breaks

Sample text formatting runs.

See Also

Reference

Run