107

Visually both of the following snippets produce the same UI. So why are there 2 controls..
Snippet1

<TextBlock>Name:</TextBlock>
<TextBox Name="nameTextBox" />

Snippet2

<Label>Name:</Label>
<TextBox Name="nameTextBox" />

(Well I am gonna answer this myself... thought this is a useful tidbit I learnt today from Programming WPF)

user7116
  • 63,008
  • 17
  • 141
  • 172
Gishu
  • 134,492
  • 47
  • 225
  • 308

6 Answers6

118

The WPF Textblock inherits from FrameworkElement instead of deriving from System.Windows.Control like the Label Control. This means that the Textblock is much more lightweight. The downside of using a textblock is no support for Access/Accerelator Keys and there is no link to other controls as target.

When you want to display text by itself use the TextBlock. The benefit is a light, performant way to display text.

When you want to associate text with another control like a TextBox use the Label control. The benefits are access keys and references to target control.

Alan Le
  • 8,683
  • 7
  • 36
  • 31
  • 13
    @Kugel: I see no reference to `Label` in the linked page, so I can't see how that is a justification for a `TextBlock` being slower than a `Label`. Can you please elaborate? If a `Label` uses a `TextBlock` for rendering, as http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/ suggests, what makes a `TextBlock` slower? – Mashmagar May 09 '12 at 14:34
21

Label has an important focus handling responsibility.Its purpose is to allow you to place a caption with an access key. It has a Target property, which indicates the target of the access key. Like this...

<Label Target="{Binding ElementName=nameTextBox}">_Name:</Label>
<TextBox x:Name="nameTextBox" />

In the absence of the Target property, the Label control does nothing useful. You'll just hear a beep if you press the access key indicating 'unable to process request'

Gishu
  • 134,492
  • 47
  • 225
  • 308
  • Is there any way to get the Label that targeted to TextBox having TextBox? – Arsen Mkrtchyan Apr 04 '12 at 10:31
  • @ArsenMkrt - do you mean a Textbox containing another TextBox? If it has a unique elementName, it should work. – Gishu Apr 04 '12 at 11:47
  • No @Gishu, I mean is the only way to find Label targeted to TextBox in your example above, to enumerate visual tree? – Arsen Mkrtchyan Apr 04 '12 at 11:53
  • @ArsenMkrt - Not sure but looks unlikely. You could go from a binding target to the source (label to textbox).. but not the other way around. Why do you need this ? – Gishu Apr 05 '12 at 06:03
  • I want to log user activity in my form, and want to have some unique identifier for every UI element, was thinking about to make identifier from label content in case of there is no Name, but I guess not a good idea, something like I described here http://stackoverflow.com/questions/9996145/is-there-any-unique-identifier-for-wpf-uielement – Arsen Mkrtchyan Apr 05 '12 at 06:44
  • @ArsenMkrt - Isn't the ViewModel a better place to have that logic - tracking actions. If the user updates a field.. you could log in the setter of the bound VM property. – Gishu Apr 05 '12 at 06:46
  • My ideal goal is to create some log that can easaly be converted to UI autmation script, tracking VM activity can be good idea too...will think about it a bit today, Thanks! – Arsen Mkrtchyan Apr 05 '12 at 06:54
3

The two biggest reasons for the confusion regarding TextBlocks and Labels are Windows Forms and common sense.

  1. When you wanted to slap a small bit of text on your form in Windows Forms, you used a Label, so it follows (incorrectly) that you would do the same thing with a WPF Label.

  2. Common sense would lead you to believe that a Label is lightweight and a TextBlock isn't, when the opposite is true.

Note that you can put a TextBlock inside a Label.

Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
2

Label takes all kinds of data inputs like String, Number etc... TextBlock, as the name suggests, only accepts a Text string.

iYadav
  • 172
  • 1
  • 3
  • 14
2

With TextBlock we can easily have multi-line support I guess - using TextWrapping.

Using Label in such cases, e.g. displaying validation message, need to use <AccessKey> tags, which is less straight-forward than TextBlock.

On the other hand, using TextBlock not allow us to set the BorderBrush property.

So, to me, the two controls should be combined into a text-full-feature control.

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • Except that TextBlock is not a control, thus there's a pretty big reason against combining them. – Joey Apr 07 '17 at 14:28
-9

Label can be used as an alternative to TextBlock for situations where minimal text support is required such as the label for a control. Using Label can be advantageous because it requires even less resources (lighter weight) then a TextBlock.

  • 8
    This is incorrect. TextBlock is the simpler (lightweight) among the two. It derives from FrameworkElement.. See top-rated answer. – Gishu Jul 09 '09 at 14:50