Distinguish TextBlock and label in WPF

Source: Internet
Author: User

TextBlock and label are used to display a small amount of data. Many articles describe the existence of a label as it allows you to use quick access. "Quick access" allows you to use ALT plus other keystrokes to interact with a control on the UI interface, such as you can use ALT plus o to click on an OK button.

TextBlock directly inherits from FrameworkElement, and the label inherits from ContentControl. In this way, label can do something like this:

1. You can define a control template (through the template property)

2. Information other than string can be displayed (through the Content property)

3. Add a dataitemplate to the label content (via 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 label.

When the label is disabled, its content becomes dimmed because there is a trigger in the default template for label, which sets the color of the content when the label is disabled.



If you want to change the style that the label disables, you can change it here.

Label is more complex than TextBlock

Above said label is equivalent to TextBlock advantage, below say the advantage of TextBlock

Loading a label takes more time than TextBlock, not least that the label has more hierarchical inheritance than the TextBlock directly inherited from FrameElement, and its visual tree is more complex.

The following picture tells you what happens backstage when you create a label.




TextBlock's 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. So it seems that label is actually a personalized TextBlock.



Add:

Both TextBlock and label can display text, which is a more commonly used control in WPF. When I first contacted WPF, I was often puzzled about how to choose between the two controls. As you learn more about WPF, you have some understanding of these two controls. Let's say some of my thoughts on TextBlock and label today.

Both the label and the TextBlock are classes under the System.Windows.Controls namespace, but their parent classes are not the same. TextBlock inherits from System.Windows.FrameworkElement, from this point of view, TextBlock cannot be called a "control" (since it does not inherit the controls class, with regard to this, I will be in the WPF Unleashed the fourth chapter for you to introduce), and the label inherits from System.Windows.ContentControl. FrameworkElement is a very low-level class, and it is also the parent class of ContentControl. Therefore, the label relative TextBlock more advanced, it can complete TextBlock can not complete the work. For example, for access key support, and we can place any object within the label, and TextBlock can only display text.

Now we look at the difference between the two from the perspective of visual tree (Luna Theme):

Label TextBlock

As you can see from the diagram, the label control consists of three elements, the bottom element of which is TextBlock. And TextBlock's visual tree is only itself. So you can say that the label control contains TextBlock.

Then look at the difference between the two from the perspective of the template. The first is the template for the label:

<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 came 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 obvious 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 previous visual tree. Now continue to ControlTemplate this topic, the label ControlTemplate contains a property trigger (for the property trigger knowledge, you can refer to my previous article), the meaning of the trigger is: when the isenabled of the label changes, Its foreground color will change, and TextBlock does not have this feature.

From the above analysis, 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 display and does not provide support for access key.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.