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 ...
随机推荐
- MYSQL复制的几种模式
MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制. MYSQL复制的几种模式 MySQL 5.1 中,在复制方面的改进就是引进了新的复制技术:基于行的复制.简言之,这种新技 ...
- RMAN duplicate from active遇到ora-17167,ora-12154
最近在从活动数据库进行异机克隆时碰到了ORA-17629,ORA-17627,ORA-12154的错误,起初以为是一个Bug呢.Oracle Bug着实太多了,已经成了习惯性思维了.汗!错误提示是无法 ...
- NodeJS学习笔记之MongoDB模块
其中还有,nodejs远程连接mysql数据库 一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于 ...
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weizhulin.blog.51cto.com/1556324/311450 大 ...
- 支持 MBTiles 规范的预缓存
SuperMap iServer 支持生成符合MBTiles规范的预缓存(MBTiles是由MapBox制定的一种将瓦片地图数据存储到SQLite数据库中并可快速使用,管理和分享的规范. 该规范由Ma ...
- 简单的聊天程序,主要用到的是Socket
服务端: import java.io.*; import java.net.*; import java.util.*; public class ChatServer { boolean stat ...
- [Everyday Mathematics]20150218
设 $A,B$ 是 $n$ 阶复方阵, 适合 $$\bex A^2B+BA^2=2ABA. \eex$$ 试证: 存在 $k\in\bbZ^+$, 使得 $(AB-BA)^k=0$.
- YII 快速创建项目GII
Yii 是一个基于组件.纯OOP的.用于开发大型 Web 应用的高性能PHP框架. 它将Web编程中的可重用性发挥到极致,能够显著加速开发进程 .Yii适合大流量的应用,如门户.BBS.CMS及B2B ...
- javascript注意点(1)
1.void运算符 ECMAScript 262规范,关于void说明如下: The void Operator The production UnaryExpression : void Unary ...
- 【quick-cocos2d-x】Lua 面向对象(OOP)编程与元表元方法
版权声明:本文为博主原创文章,转载请注明出处. 面向对象是一种对现实世界理解和抽象的方法,是计算机编程技术发展到一定阶段后的产物. 早期的计算机编程是基于面向过程的方法,通过设计一个算法就可以解决当时 ...