TextBlock and labels are used to display a small amount of data. Many articles describe the existence of a label as it allows for "quick access". Quick access allows you to use ALT plus other keys to quickly interact with a control in the UI, such as you can click an OK button with ALT + O key.
TextBlock directly inherits from FrameworkElement, and the label inherits from ContentControl. So it seems that label can do something like this:
1. You can define a control template (via the template property)
2. Can display information other than string (via the Content property)
3. Add a dataitemplate to the label content (via the ContentTemplate property)
4. Do something that frameworkelement elements can't do
Below is an inheritance diagram of a TextBlock and a label
When the label is unavailable, its text appears dimmed, but TextBlock does not
In the example above, username is textblock,password as a label.
When the label is disabled, its content turns gray because there is a trigger in the label's default template, which sets the color of the content when the label is disabled.
If you want to change the style when the label is disabled, you can change it.
Label is more complex than TextBlock
The above said that the label is equivalent to the advantages of TextBlock, the following is about TextBlock advantage
It takes more time to load a label than a TextBlock, and it is more complex than a label that has more hierarchical inheritance than the TextBlock directly inheriting from FrameElement.
The picture below tells you what's going on in the background when you create a label.
The TextBlock visual tree does not contain any child elements, but the label is much more complex. It has a border property and finally displays the content through a TextBlock. It seems that the label is actually a personalized TextBlock.
Add:
Both TextBlock and label can display text, which is a more common control in WPF. When I first approached WPF, I was often confused about how to select the two controls. As you learn more about WPF, there is some understanding of both controls. Tell me something about TextBlock and label today.
Both the label and the TextBlock are classes under the System.Windows.Controls namespace, but the parent class is not the same. TextBlock inherits from System.Windows.FrameworkElement, from this point of view, TextBlock can not be called "control" (because it does not inherit the control class, about the control class, I will be in the WPF Unleashed the fourth chapter for everyone), and label inherits from System.Windows.ContentControl. FrameworkElement is a very low-level class, and it is also the parent of ContentControl. So, the label is a bit more advanced than TextBlock, and it can do the work that TextBlock can't do. For example, access key support, and we can place any object within the label, and TextBlock can only display text.
Now let's look at the difference between the two from the perspective of the visual Tree (Luna Theme):
Label TextBlock
As you can see, the label control consists of three elements, the lowest element of which is TextBlock. And TextBlock's visual tree is only its own. So it can be said that the label control contains TextBlock.
Then look at the difference between the two from the template perspective. The first is the label template:
<style targettype= "{x:type Label}" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= " Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:s=" Clr-namespace:system;assembly=mscorlib ">
<Style.Resources>
<resourcedictionary/>
</Style.Resources>
<setter property= "Textelement.foreground" >
<Setter.Value>
<dynamicresource resourcekey= "{x:static Systemcolors.controltextbrushkey}"/>
</Setter.Value>
</Setter>
<setter property= "Panel.background" >
<Setter.Value>
<SolidColorBrush>
#00FFFFFF </SolidColorBrush>
</Setter.Value>
</Setter>
<setter property= "Control.padding" >
<Setter.Value>
<Thickness>
5,5,5,5</thickness>
</Setter.Value>
</Setter>
<setter property= "Control.horizontalcontentalignment" >
<Setter.Value>
<x:static member= "HorizontalAlignment.Left"/>
</Setter.Value>
</Setter>
<setter property= "Control.verticalcontentalignment" >
<Setter.Value>
<x:static member= "Verticalalignment.top"/>
</Setter.Value>
</Setter>
<setter property= "Control.template" >
<Setter.Value>
<controltemplate targettype= "{x:type Label}" >
<border borderbrush= "{TemplateBinding Border.borderbrush}" borderthickness= "{TemplateBinding border.borderthickness} "background=" {TemplateBinding panel.background} "snapstodevicepixels=" True "padding=" { TemplateBinding control.padding} ">
<contentpresenter horizontalalignment= "{TemplateBinding control.horizontalcontentalignment}" VerticalAlignment = "{TemplateBinding control.verticalcontentalignment}" snapstodevicepixels= "{TemplateBinding Uielement.snapstodevicepixels} "contenttemplate=" {TemplateBinding contentcontrol.contenttemplate} " recognizesaccesskey= "True" content= "{TemplateBinding contentcontrol.content}"/>
</Border>
<ControlTemplate.Triggers>
<trigger property= "uielement.isenabled" >
<setter property= "Textelement.foreground" >
<Setter.Value>
<dynamicresource resourcekey= "{x:static Systemcolors.graytextbrushkey}"/>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>
False</s:boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Next comes the TextBlock:
<style targettype= "{x:type TextBlock}" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x= "Http://schemas.microsoft.com/winfx/2006/xaml" >
<Style.Resources>
<resourcedictionary/>
</Style.Resources>
<setter property= "Textblock.textwrapping" >
<Setter.Value>
<x:static member= "Textwrapping.nowrap"/>
</Setter.Value>
</Setter>
<setter property= "Textblock.texttrimming" >
<Setter.Value>
<x:static member= "Texttrimming.none"/>
</Setter.Value>
</Setter>
</Style>
It is clear from two pieces of code that the template for the label is more complex and that the TextBlock control has no ControlTemplate part, which is consistent with the previous visual tree. Now continue to ControlTemplate this topic, the label's ControlTemplate contains a property trigger (about the property trigger knowledge, you can refer to my previous article), the meaning of this trigger is: when the label's isenabled changes, Its foreground color changes, and TextBlock does not have this feature.
From these analyses, it can be concluded that TextBlock belongs to the lower-level control, so its performance is better than the label. TextBlock is a good choice if the requirement is only plain text and does not provide support for access key. Transferred from: http://www.cnblogs.com/junbird-nest/archive/2012/10/08/2715601.html
[Go] differentiate TextBlock and label in WPF