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

5

27

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?

Seamaid answered 9/5, 2011 at 7:50 Comment(0)
L
61

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.

Laetitia answered 25/8, 2011 at 12:17 Comment(2)
Holy crap. xml:space="preserve" is quite handy. Have a +1Gonococcus
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.Anion
E
9

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.

Electromotive answered 14/9, 2011 at 17:0 Comment(1)
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).Gromyko
T
7

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.

Transmissible answered 9/5, 2011 at 7:58 Comment(3)
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.Seamaid
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.Seamaid
I would like to see if you can set xml:space="preserve" in code or if that attribute could be set to a run.Layard
S
3

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

<TextBlock xml:space="preserve">Staff Contact Details        <Hyperlink>Click here</Hyperlink></TextBlock>
Strained answered 8/7, 2021 at 9:20 Comment(0)
Z
1

Another solution is using the Run class like this:

<TextBlock>
    <Run Text="    "/>
    Hello world!
</TextBlock>
Zareba answered 6/12, 2023 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.