开发者

Silverlight TextBlock Text Attribute vs Content

开发者 https://www.devze.com 2023-01-18 11:36 出处:网络
When would I use the Text attribute of the <TextBlock> and when should I put my text in the content of the <TextBlock>?

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>Ex开发者_开发百科ample Text</TextBlock>


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消