WPF Learning Supplements (ii) TextBlock line break

Source: Internet
Author: User

Original: WPF Learning Supplements (ii) TextBlock line break

In the afternoon to help colleagues in the group to solve a small problem, for later convenience, put it to collect it.

Create a new TextBlock as the most basic control, he carries the function compared to other controls to come to less,

The more notable attributes are out of the layout with the exception of the data binding, and only the Text property is left.

The default content property for TextBlock is text.

For line breaks, you need to focus on the text and Inline2 properties.

Common TextBlock Line Wrapping method

1) Escape character wrapping

Adding code to a background file in XAML

1 Me "  ""bbbbbbbb "
1 " aaaaaaa\nbbbbbbbb ";

When writing on XAML

 <  textblock  text  = "aaaaaaa& #x000A; BBBBBB "  Height  =" 198 "  HorizontalAlignment  = "left"   = "30,27,0,0"   Name  = "TextBlock1"   VerticalAlignment  = "Top "  Width  =" 436 " >  </ textblock  >  

This will indicate that the displayed presentation is wrapped.

However, the following scenario is not valid.

        <TextBlockHeight= "198"HorizontalAlignment= "Left"Margin= "30,27,0,0"Name= "TextBlock1"VerticalAlignment= "Top"Width= "436" >aaaaaaa& #x000A;bbbbbb</TextBlock>

When assigning values in the text, they are parsed according to XML, so the escape character for the XML will play their part, and this is the case, although the notation is

is to get the content property of TextBlock by default, but during the assignment the convert is processed by a string, and when resolved to a newline character in the string, the default turns into a space.

So you see the results aaaaaaa and bbbbbb are not wrapped in a line, just a space in between.

Another escape character for XML

Spaces (& #x0020;)
Tab (& #x0009;)
Enter (& #x000D;)

line Break (& #x000A;)

2) Set properties, wrap line

Because TextBlock has the support of the TextWrapping property, it automatically sets the line wrapping according to the width of the control, which basically satisfies the requirement when there is no special requirement.

        <TextBlockHeight= "$"HorizontalAlignment= "Left"Margin= "30,27,0,0"Name= "TextBlock1"VerticalAlignment= "Top"Width= "$"textwrapping= "Wrap">aaaaaaabbbbbb</TextBlock>

3) Use the linebreak of the Inlines property to break the line

Simply put, the displayed XAML representation is

        <TextBlockHeight= "$"HorizontalAlignment= "Left"Margin= "30,27,0,0"Name= "TextBlock1"VerticalAlignment= "Top"Width= "$"textwrapping= "Wrap">             <Run>Aaaaaa</Run>            <LineBreak/>            <Run>bbbbbb</Run>        </TextBlock>

Background code add

        TEXTBLOCK1.INLINES.ADD (New Run ("aaaaaa"))        TextBlock1.Inlines.Add (  New  LineBreak ())        TextBlock1.Inlines.Add (new Run ("BBBB "))

In addition, if the run has a large number of words, more than the width of the TextBlock, the text will be cut off, instead of automatic line, this need to pay attention to

WPF Learning Supplements (ii) TextBlock line break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.