tags:

views:

64

answers:

2

When would I use the Text attribute of the <TextBlock> and when should I put my text in the content of the <TextBlock>?

<TextBlock Text="Example Text" />

            vs.    

<TextBlock>Example Text</TextBlock>
+3  A: 

The former can be bound, whilst the latter is particularly useful when combining Runs:

<TextBlock Text="{Binding SomeProperty}"/>
<TextBlock>
    <Run>You have </Run>
    <Run Text="{Binding Count}"/>
    <Run>items.</Run>
</TextBlock>

HTH,
Kent

Kent Boogaart
+3  A: 

The use of the Text property has become common as a result of previous versions of the Xaml parser but the placing the text as content is more natural especially if you have a background in HTML.

The fact the many TextBlocks either have simple short chunks of literal text in or are bound. Would tip the balance IMO to using the Text property. In addition any globalisation that may come along latter may end with those literals being replaced by bindings as well.

AnthonyWJones
AnthonyWJones is quite right: Localisation is a key factor here. In practice it usually means all fancy multiple value insertion is actually done elsewhere and only a single resulting text value is bound to the Text property. While the example with multiple runs is "ok" for hard-code English, it is not suitable for commercial applications.
Enough already
@HiTech: Anthony's point is certainly valid, but I'm calling BS on "not suitable for commercial applications". That depends entirely on whether globalisation is a requirement of said application, which is it often is not.
Kent Boogaart
@Kent Boogaart: *Every* application we develop nowadays should be expected to support localisation. To assume otherwise is a little short-siighted. Should we really be encouraging bad habits (e.g. hard-wiring text) for newbies? Sorry, but unless you have a better reason that *"we don't always need to localise"* I stand by my original comment.
Enough already
@HiTech: sorry, but that's ridiculous. I could make the same "just-in-case" argument to justify all manner of wasteful effort. The reason is simple: globalization and localization takes extra effort, so if you know for a fact that the effort is unnecessary, you're wasting your own time and your client's money by supporting it. Of course, you should always make the trade-off clear to your client.
Kent Boogaart
@Kent Boogaart: *Allowing for the possibility of localisation* is not the same as actually implementing it. Back to my original point *If* you use runs, you cannot easily localise those strings. That's all. End of story. No extra time or cost to me or my clients. I have localised many dozens of apps, including legacy app, so you cannot possibly convince me to go back to hard-coding text *anywhere* :)
Enough already
"I have localised many dozens of apps". Exactly - and you will have gotten better at with time and practice. I highly doubt you were as efficient the first time you did it as you are now. I understand the difference between globalization and localization. I admit I have not had to do much localization, but I do tend to make stuff "globalizable" where I see no extra effort to do so. To me though, sticking some text directly inside a TextBlock is quicker and easier than "doing it elsewhere". We may have to agree to differ on this one :)
Kent Boogaart
+1 for the globalization point
amurra