Using the Run and LineBreak Objects

,

In XAML, whitespace is interpreted in the same way as in XML. Whitespace is collapsed, leading and trailing whitespace is removed, and multiple lines are placed on the same line. The LineBreak class represents an explicit new line in a TextBlock. To create a multiline TextBlock, use the LineBreak element within the content, as shown:

<TextBlock Style="{StaticResource LabelTextStyle}">
    You are reading<LineBreak />
    Windows Phone 8 Unleashed!
</TextBlock>


Note

The XML-like format of XAML does not allow you to use the greater than > or less than < symbols within XAML. This is because the symbols are interpreted as being part of an element. Instead, use the character entity references: &gt; and &lt;. These are translated to the greater than and less than characters at runtime.


Run elements can be embedded within the TextBlock content so that different font formatting can be applied to different parts of the text. LineBreak elements are generally used to delimit surrounding Run elements.

LineBreak and Run are derived from System.Windows.Documents.Inline. The TextBlock class contains a strongly typed InlineCollection called Inlines, which contains the items to be displayed (see Figure 6.2).

Image

FIGURE 6.2 The TextBlock has a collection of Inline objects.

Inlines is the XAML content property of the TextBlock class. Thus, to specify items in the TextBlock, you specify various Run and LineBreak elements as child elements of the TextBlock.

The following example shows how to display several strings of differing format in a TextBlock using Run elements separated with LineBreak elements.

<TextBlock>
    <Run Foreground="Blue" FontFamily="Courier New"
         FontSize="18">You are reading</Run><LineBreak/>
    <Run Foreground="Teal" FontFamily="Times New Roman"
         FontSize="24" FontStyle="Italic">Windows Phone 8 Unleashed!</Run>
</TextBlock>

The result is shown in Figure 6.3.

Image

FIGURE 6.3 Using Run and LineBreak objects within a TextBlock.


Note

Unfortunately the Run.Text property is not a DependencyProperty, which prevents using data binding with the property. If you attempt to place a binding on the Text property in XAML, a difficult to diagnose error will be raised at runtime.

A solution that allows you to display a mixture of static text and text populated via data bindings is to use multiple TextBlock elements within a StackPanel, with the StackPanel element’s Orientation set to Horizontal.



Note

When attempting to provide different formatting for a contiguous string of text, in XAML, Run elements should be placed side by side without separating whitespace because even though they appear to be properly laid out in the Visual Studio designer, at runtime whitespace can appear between the Run elements.


LineBreak forces the text in each Run to display on a separate line. Without the LineBreak, the text in each Run would be displayed on the same line. This can cause the text to be clipped if it exceeds the TextBlock width or the width of the parent container.

To avoid the clipping without using a LineBreak, set the TextBlock TextWrapping to Wrap, like so:

<TextBlock TextWrapping="Wrap" />

Better still, create a reusable Style for your TextBlock, like so:

<Style x:Key="NormalTextStyle" TargetType="TextBlock"
        BasedOn="{StaticResource PhoneTextNormalStyle}">
    <Setter Property="TextWrapping" Value="Wrap" />
</Style>

The Style can be placed in the Resources element of your page or app.xaml file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.200.164