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:

  1. Be given a custom control template (via the Template property).
  2. Display data other than just a string (via the Content property).
  3. Apply a DataTemplate to its content (via the ContentTemplate property).
  4. 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

Label Class

TextBlock Class

AccessText Class

ContentPresenter.RecognizesAccessKey Property

How to: Create a Control That Has an Access Key and Text Wrapping

Snoop (a WPF utility)

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的更多相关文章

  1. [转]在WPF中区别TextBlock和Label

    TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...

  2. WPF学习二:TextBlock和Label的区别

    TextBlock和Label都是用来显示少量数据的.好多文章对Label存在的描述都是它允许使用"快速获取"."快速获取"就是允许你用Alt加上其它的按键快速 ...

  3. Win10 UWP 开发系列:使用SplitView实现汉堡菜单及页面内导航

    在Win10之前,WP平台的App主要有枢轴和全景两种导航模式,我个人更喜欢Pivot即枢轴模式,可以左右切换,非常方便.全景视图因为对设计要求比较高,自己总是做不出好的效果.对于一般的新闻阅读类Ap ...

  4. 【WPF系列】-TextBox常用知识点

    DataBinding中更新数据源的时机   TextBox .Text 属性的默认 UpdateSourceTrigger 值为 LostFocus.这意味着如果应用程序的 TextBox 包含数据 ...

  5. (WPF) 基本题

    What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ...

  6. Wix 安装部署教程(九) --用WPF做安装界面

    经常安装PC端的应用,特别是重装系统之后,大致分为两类.一类像QQ,搜狗输入法这样的.分三步走的:第一个页面可以自定义安装路径和软件许可.第二个页面显示安装进度条,第三个页面推荐其他应用.先不管人家怎 ...

  7. WPF快速入门系列(3)——深入解析WPF事件机制

    一.引言 WPF除了创建了一个新的依赖属性系统之外,还用更高级的路由事件功能替换了普通的.NET事件. 路由事件是具有更强传播能力的事件——它可以在元素树上向上冒泡和向下隧道传播,并且沿着传播路径被事 ...

  8. WPF自学笔记

    WPF使用哪几种元素作为顶级元素: 1. Window元素 2. Page元素(与Window元素类似,用于可导航的应用程序) 3. Application元素(定义应用程序资源和启动设置) PS:在 ...

  9. WPF打印京东电子面单(可以异步)

    参考:https://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.html 模板:JDFlowDocument.xaml <FlowD ...

随机推荐

  1. 【英语】Bingo口语笔记(51) - 相信怀疑的表达

  2. MySQL多表连接

    主要分3种:内连接,外连接,交叉连接 其        他:联合连接,自然连接 1.内联接 典型的联接运算,使用像 =  或 <> 之类的比较运算).包括相等联接和自然联接. 内联接使用比 ...

  3. [转载] 老版本ubuntu 更新源

    untu的普通版本支持的时间都有限,过了支持的时间,更新源都会被停用,比如ubuntu9.10原来的源都失效了(包括官方源,类似ustc的第 三方源,因为这些第三方源也是和官方源同步的).因此,直接用 ...

  4. 如何调试Android Framework?

    Linus有一句名言广为人知:Read the fucking source code. 但其实,要深入理解某个软件.框架或者系统的工作原理,仅仅「看」代码是远远不够的.就拿Android Frame ...

  5. 使用git自动将子工程发布到百度开放云上

    我的项目中包含多个子工程,如web工程.python工程等.我在项目的根目录下建立了git管理,因此如果使用git push只能把整个项目推送上去,但我只想推送web工程目录.因此,编写了cmd脚本如 ...

  6. 数据绑定表达式(上):.NET发现之旅(一)

    数据绑定表达式(上):.NET发现之旅(一) 2009-06-30 10:29:06 来源:网络转载 作者:佚名 共有评论(0)条 浏览次数:859 作为.NET平台软件开发者,我们频繁与各种各样的数 ...

  7. C#隐式运行CMD命令(隐藏命令窗口)

    原文 C#隐式运行CMD命令(隐藏命令窗口) 本文实现了C#隐式运行CMD命令的功能.下图是实例程序的主画面.在命令文本框输入DOS命令,点击"Run"按钮,在下面的文本框中输出运 ...

  8. bjfu1262 优先队列

    比较典型的应用优先队列的题.题目是在一个长为n的数组中,依次问m个数中的最小值.那么把值和下标做成一个结构体,放进优先队列里,每次移动窗口就把该T的T掉,剩下的最小值就是答案,复杂度nlogn,轻松a ...

  9. <转>安卓软件测试的几个要点

    1.界面 ① 文字错误.图片不显示或显示不正确.缺少输入项.按钮的大小和点击效果 ② 布局.图片和配色设计问题,测试人员很难进入 ③ 提示信息,提示信息语言准确简洁,有指导性.在应该提示的位置放入提示 ...

  10. ansible中文手册-基础模块使用

    此篇文章主要是翻译ansible官网文档而来,在里面讲述了如何使用ansible的基础模块,总体感觉比较晦涩,但是后面会写出自己相关实践的文档,从而更加通俗易懂,官网的东西拿来当手册偶尔翻翻也是很不错 ...