TextBlock versus Label

29 Apr

Some thoughts on the use of TextBlock versus Label in Silverlight.

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 funcationlity provided by a Label you should use a TextBlock.

TM
http://thomasmartinsen.net

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: