TextBlock controls
The TextBlock control is one of the most frequently used controls during application development, and its primary function is to display a read-only text content. Developers can use the TextBlock control to display prompt information, as well as to set the displayed cue information to a different size and style, depending on the requirements.
In a XAML file, the use of the TextBlock control is as follows:
<textblock?... >
<!-- Add text -
</TextBlock>
- or -
<textblock?... >
<!-- add one or more Run Elements -
</TextBlock>
- or -
<textblock?.../>
The following describes the common properties of the TextBlock control:
- FontFamily property, Gets or sets the font that displays text in the TextBlock control.
- The FontSize property, Gets or sets the size of the displayed text in the TextBlock control, by default, in pixels.
- The FontStyle property, Gets or sets the font style of the displayed text in the TextBlock control, whose property values can be normal,italic and oblique.
- The Text property, gets or sets the textual content that is displayed in the TextBlock control.
- Foreground property, Gets or sets the color of the text displayed in the TextBlock control.
- TextWrapping property, Gets or sets whether the text in the TextBlock control wraps automatically. If the property value is wrap, the text wraps, and if the property value is nowrap, the line is not wrapped.
The TextBlock control's child elements run and linebreak can be used to fine-grained the text content, and the run element can set the properties and style of the text, and the linebreak element represents wrapping in the TextBlock control.
An example is followed to illustrate how the TextBlock control is used.
Create a new Windows store blank application project and name Textblockdemo to add the following code to the grid element of the MainPage.xaml file.
<textblock horizontalalignment= "left" fontsize= "margin=" 644,271,0,0 "textwrapping=" Wrap "text=" TextBlock Control Example "verticalalignment=" Top "width=" 224 "height="/>
<textblock fontsize= "margin=" "644,314,498,370" >
<run fontfamily= " italics "> this is </Run>
<run fontfamily= " italics "> a text in italics </Run>
<LineBreak/>
<run fontfamily= " Song Body "> This is a text of the song Body </Run>
</TextBlock>
In the preceding code, two TextBlock text blocks were added, and in the first block of text, the value of their FontSize property value to the 25,text property is "TextBlock Control Example". In the second block of text, define the value of the FontSize property to 25, and then add three run elements to the text block, set the first two run elements of this content to "this is" and "a text in italics", the FontFamily attribute value is "italic", Then add a linebreak element for wrapping the text. In the third run element , the text content is "This is a text in Arial", andthe FontFamily property value is "Arial".
Running the program, the text content written in the TextBlock control will be displayed in the font set in the interface, as shown in effect 4-22.
Figure 4-22 using the TextBlock control to show different text effects
WIN10 series: C # App Control Basics 12