Difference between WPF Textblock and Label Control

+1 vote
asked Jul 7, 2013 in WPF by anonymous

We are developing WPF application in our organization recently. So, having big learning curve in WPF. I am really not sure why we do have Textblock control as a new control when we also have Label control. Both label and textblock used to display text, but not editable. If so, what is the purpose of textblock? when should I use label over the other.


1 Answer

+1 vote
answered Jul 8, 2013 by Aadhira (1,213 points)
selected Jul 8, 2013 by administrator
Best answer

Textbox and Label control both are most of the time used for same purpose. But, the label has other additional capabilities. In fact, Label control is internally using textblock control.
Label is basically derived from Content Control but the textblock is a Framework element. Even though the text block is a control it is not really a control. Templates can be applied only in content control not on Framework element. So, using text block, we can only display string. But with Label you can display anything as content (Images, multiple controls etc).

Even though we can use label to display more than just a string, I would not recommend using label to display complex content. Because we have other specific elements to does that job.

The only time you want to use Label control over textblock would be, label can used to set focus on the control with which it is being used. i.e If you use a lable control to display text “Firstname” and if there is a corresponding textbox to get user input for the “Firstname”, then you can use label to set the focus on that textbox by setting the target element name, whenever the “ALT” + access key.

In the below example, label has access key as “ALT + F”. Once the user use this access key, focus will automatically be set to the txtFirstName textbox. It is done by using “Target” property on the label control to target the textbox xontrol’s name.

<Window x:Class="WpfDemo.LabelDemo"
        Title="LabelDemo" Height="300" Width="300">
        <Label Content="_Firstname" Target="{Binding ElementName=txtFirstname}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,40,0,0"/>
        <TextBox x:Name="txtFirstname" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="130,40,0,0"/>

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com