Silverlight TextBlock versus Label

8 Jun

Some thoughts on the use of TextBlock versus Label.

Even though TextBlock lives in the System.Windows.Controls namespace, it is not a control.  It derives directly from FrameworkElement.  Label, on the other hand, derives from ContentControl.  This means that Label can:

1. Be given a custom control template (via the Template property).
2. Display data other than just a string (via the Content property).
3. Apply a DataTemplate to its content (via the ContentTemplate property).
4. Do whatever else a ContentControl can do that a FrameworkElement cannot.

But be aware! The use of Label is a way more heavy then TextBlock due to its Visual Tree.

Visual Tree of a Label:
<Label>
<Border>
   <ContentControl>
     <ContentPresenter>
       <Grid>
         <TextBlock Text="Hello World"/>
       </Grid>
     </ContentPresenter>
   </ContentControl>
</Border>
</Label>

Visual Tree of a TextBlock:
<TextBlock Text="Hello World" />

So unless you really need the functionality provided by a Label (see below) you should use a TextBlock.

 

Happy Coding!
Thomas Martinsen

4 Responses to “Silverlight TextBlock versus Label”

  1. Klay November 13, 2009 at 15:44 #

    Since when is there a Label control in Silverlight?

  2. Gideon November 25, 2010 at 18:46 #

    Grrr.. I installed the toolkit with the dev tools and never realized label was not part of the basic controls! Jeez! I have labels all over the place now instead of textblocks! =(

    Thanks for your article!

  3. Jens Fiederer July 3, 2012 at 16:16 #

    see below?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: