Set TextBlock to preserve white space at the beginning and at the end?

18,404

Solution 1

In this case you don't need to use xml:space="preserve"

<TextBlock xml:space="preserve" Text="     Hello world!    " />

WILL display the whitespaces, however

<TextBlock>    Hello world!    </TextBlock>

won't.

Solution 2

Re: "I just hope you are not using this to align your text. There are many other more elegant methods to do so."

Sounds like you might want to use the Padding property: http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.padding(VS.85).aspx.

See also the various alignment & margin properties.

Solution 3

set the xml:space property to preserve in your XAML, i assume you are using WPF

<TextBlock xml:space="preserve" Text="     Hello world!    " />

EDIT: It is sometimes easier to do things in XAML. I just hope you are not using this to align your text. There are many other more elegant methods to do so.

Solution 4

You don't need to use the Text= property. This also works:

<TextBlock xml:space="preserve">Staff Contact Details        <Hyperlink>Click here</Hyperlink></TextBlock>
Share:
18,404

Related videos on Youtube

Rasto
Author by

Rasto

Entrepreneur, UX consultant, full-stack developer with strongest competency in Swift &amp; iOS. Can get die-hard about top-notch UX and code quality. Diving deeper into React and getting excited about GraphQL. My pro history chapters are written in C#, Objective-C, Java, JS, TS, Flow, even Haskel, Prolog &amp; Pascal. Sports and outdoor enthusiast. Love exploring cultures around the world. Found in mountains in the winter and at seaside during summer. Amater photographer. Always happy to learn and share what I learned e.g. externally giving lectures at my alma mater.

Updated on July 08, 2021

Comments

  • Rasto
    Rasto almost 3 years

    EDIT:

    The code below actually works as I want - this question a little misleading. Please ignore it.


    Normally when I set Text property of TextBlock like this:

    TextBlock tb = new TextBlock();
    tb.Text = "     Hello World ";
    

    The whitespace at the beginning and at the end of text are not shown. The text shown by TextBlock is only Hello World. How can I set TextBlock to display them (i.e., not remove the whitespace)? Am I missing some property?

  • Rasto
    Rasto about 13 years
    Sorry but -1. The white space is not removed because of XAML - the same think will happen if you set it in code. I intentionally gave example in code. I guess this is done by TextBlock itself.
  • Rasto
    Rasto about 13 years
    So sorry. You are right. In code it does not trim it. I cannot cancel my downvote until you edit your answer :( So if you care about votes please edit your answer just a little so I can replace downvote by upvote.
  • Benjamin
    Benjamin almost 8 years
    Holy crap. xml:space="preserve" is quite handy. Have a +1
  • Sam
    Sam over 7 years
    I agree with Rob, however, I can see the usefulness of xml:space="preserve". For example, if you are using ViewBox, margin or padding are hard to use as the font size may vary (hence the space size).
  • Chris
    Chris over 5 years
    This doesn't seem to work for extending TextDecorations, the space at the beginning is there and decorated, but the text at the end is undecorated (it's still there though). It will work if you use non-breaking spaces though.
  • Deathstalker
    Deathstalker about 4 years
    I would like to see if you can set xml:space="preserve" in code or if that attribute could be set to a run.