Label & TextBlock
I always thought it was odd that WPF has both TextBlock and Label. They both are responsible for displaying a small amount of text. Every piece of documentation about Label I have read justifies its existence by mentioning that it allows for access keys. Access keys allow you to hit Alt + SomeKey to quickly interact with a control in the UI, such as hitting Alt + O to click an “OK” button. My thought has been, “Why not just add support for access keys to TextBlock and get rid of Label?”
Recently I discovered some reasons why it makes sense for Label to exist.

The “Username:” text is a TextBlock and the “Password:” text is a Label.
TextBlock is not a control
Even though TextBlock lives in the System.Windows.Controls namespace, it is not a control. It derives directly from FrameworkElement. Label is control, on the other hand, derives from ContentControl. This means that Label can:
- Be given a custom control template (via the Template property).
- Display data other than just a string (via the Content property).
- Apply a DataTemplate to its content (via the ContentTemplate property).
- Do whatever else a ContentControl can do that a FrameworkElement cannot.
Below is a fragment of the class inheritance hierarchy containing Label and TextBlock:

Label text is grayed out when disabled
When a Label’s IsEnabled property returns false its text is “grayed out.” TextBlock does not have this behavior by default. Here is what the demo app looks like when the input area is disabled. Keep in mind that the “Username:” text is a TextBlock and the “Password:” text is a Label:

The reason Label text turns gray when it is disabled is due to the fact that Label’s default control template has a Trigger which explicitly sets the Foreground property when IsEnabled is false. Here is that Trigger from Label’s default control template:

If we really wanted TextBlocks to appear grayed out when disabled, we could repurpose that XAML seen above into a Style which is applied to TextBlocks (as seen in the demo project available at the end of this post).
Label supports access keys
This is the standard explanation of why Label exists. You can associate a Label with another control, such as a PasswordBox, and allow the user to type an access key defined by the Label to set focus to the other control. The access key is represented in the UI by drawing a line under the appropriate character. If the user presses the Alt key and then the designated “access character” the target control will be given focus.
Here is what the demo application looks like after the user presses the Alt key:

Notice how every piece of text in the UI has an access key indicator, except for the “Username” TextBlock. The “Password” Label and its target (a PasswordBox) were declared like this:

Label is much heavier than TextBlock
So far we have examined why Label can be considered better than TextBlock, but now its time to discuss the benefits of using a TextBlock instead. Label has a higher runtime overhead than TextBlock. Not only does Label inherit from two classes further down the inheritance hierarchy than TextBlock, but its visual tree is much more involved.
I snooped the visual tree of the demo app to see what was really going on when you create a Label with an access key defined. Below is a screenshot of Snoop displaying the visual trees of both the “Username” TextBlock and the “Password” Label (with an access key defined):

The “Username” TextBlock’s visual tree contains no child elements. The Label, however, is much more involved. It has a Border, which contains a ContentPresenter, which hosts an AccessText element, which finally uses a TextBlock to display the text. So it turns out that using a Label is really just an elaborate and customizable way of using a TextBlock.
Links
ContentPresenter.RecognizesAccessKey Property
How to: Create a Control That Has an Access Key and Text Wrapping
Download the demo
Download the demo project here: Label vs TextBlock (demo project) Be sure to change the file extension from .DOC to .ZIP and then decompress it.
Label & TextBlock的更多相关文章
- [转]在WPF中区别TextBlock和Label
		TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ... 
- WPF学习二:TextBlock和Label的区别
		TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ... 
- Win10 UWP 开发系列:使用SplitView实现汉堡菜单及页面内导航
		在Win10之前,WP平台的App主要有枢轴和全景两种导航模式,我个人更喜欢Pivot即枢轴模式,可以左右切换,非常方便.全景视图因为对设计要求比较高,自己总是做不出好的效果.对于一般的新闻阅读类Ap ... 
- 【WPF系列】-TextBox常用知识点
		DataBinding中更新数据源的时机 TextBox .Text 属性的默认 UpdateSourceTrigger 值为 LostFocus.这意味着如果应用程序的 TextBox 包含数据 ... 
- (WPF) 基本题
		What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ... 
- Wix 安装部署教程(九) --用WPF做安装界面
		经常安装PC端的应用,特别是重装系统之后,大致分为两类.一类像QQ,搜狗输入法这样的.分三步走的:第一个页面可以自定义安装路径和软件许可.第二个页面显示安装进度条,第三个页面推荐其他应用.先不管人家怎 ... 
- WPF快速入门系列(3)——深入解析WPF事件机制
		一.引言 WPF除了创建了一个新的依赖属性系统之外,还用更高级的路由事件功能替换了普通的.NET事件. 路由事件是具有更强传播能力的事件——它可以在元素树上向上冒泡和向下隧道传播,并且沿着传播路径被事 ... 
- WPF自学笔记
		WPF使用哪几种元素作为顶级元素: 1. Window元素 2. Page元素(与Window元素类似,用于可导航的应用程序) 3. Application元素(定义应用程序资源和启动设置) PS:在 ... 
- WPF打印京东电子面单(可以异步)
		参考:https://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.html 模板:JDFlowDocument.xaml <FlowD ... 
随机推荐
- Java核心技术II读书笔记(二)
			ch2 XML 有两种XML文档结构,DTD和Schema,用解释文档构成规则,这些规则指定了每个元素俺的合法子元素和属性. DTD DTD有多种提供方式,可以像下面这样加到XML中: <?xm ... 
- 前台实现下载xml功能
			阅读目录 介绍问题 MIME TYPE 解决问题 介绍问题 平时我们通过href去链接文件时,一般情况是对于zip.jar等下载功能,而对于xml.css.html.js等都是查看功能. 现在下面的代 ... 
- Android Studio Check for Update
			Android Studio 当前版本1.0.1, 官网新版本1.1.0, 通过 Check for Update...升级, 提示 Connection failed. Please check y ... 
- 【转】ACE编程小结
			转自:http://blog.csdn.net/mjp_mjp/article/details/4406059 1.多线程中的ACE_Reactor::EventLoop,当在多线程(池)中调用Eve ... 
- N元数组的子数组之和的最大值
			题目:有N个整数的元素的一维数组,求子数组中元素之和中最大的一组(思想:动态规划) 分析: 设该数组为array[N], 那么对于array[i]该不该在元素之和最大的那个子数组中呢?首先,不如假设a ... 
- C++模板实例掌握
			前段时间重新学习C++,主要看C++编程思想和C++设计新思维.对模版的使用有了更进一层的了解,特总结如下: 下面列出了模版的常用情况: << '\n';} //参考:http://ww ... 
- 性能测试之系统监控工具nmon
			一.概述 本篇文章主要讲解nmon,以下为目录 1.nmon介绍 2.nmon下载.安装及使用 3.nmon analysis 分析及使用,各个项的含义 二.详细信息: 1.nmon介绍: nmon( ... 
- Fisher information matrix笔记
			在看FK论文时,fisher information matrix是必须理解的. 从维基百科查阅到,Fisher information matrix是用利用最大似然函数估计来计算方差矩阵. 来源于: ... 
- VS2013中修改.dll工程项目的.lib和.dll的输出路径
			一个dll工程,生成的两个东西是我们需要的:.lib和.dll,在实际开发过程中我们往往希望这两个东西直接输出到特定文件夹,对于这两个的修改: 设置好之后,对于调用该dll的exe工程,将exe的输出 ... 
- 浅谈jquery选择器
			首先来说说jquery选择器的优势: 1.简洁的写法 2.支持css1.0到3.0选择器 3.完善的处理机制. 再来说说分类: jquery选择器分为基本选择器.层次选择器.属性选择器.基本过滤选择 ... 
