标签和文本块之间的区别
根据 使用 Microsoft .NET 4 进行 Windows 应用程序开发 70 -511 培训套件
Label
控件和 TextBlock
控件之间有什么区别,因为它们都是内容控件并且仅显示文本?
According to the Windows Applications Development with Microsoft .NET 4 70-511 Training Kit
What is the difference between the Label
control and TextBlock
control since both are content controls and just displaying text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
TextBlock 不是控件
尽管
TextBlock
位于 System.Windows.Controls 命名空间中,但它不是控件。它直接派生自FrameworkElement
。另一方面,Label 派生自ContentControl
。这意味着Label
可以:Template
属性)。Content
属性)。DataTemplate
应用于其内容(通过ContentTemplate
属性)。执行
ContentControl
可以执行FrameworkElement
无法执行的其他操作。标签
文本在禁用时呈灰色标签
支持访问键Label
比TextBlock
重得多来源
下面有一些更有趣的读物
TextBlock is not a control
Even though
TextBlock
lives in the System.Windows.Controls namespace, it is not a control. It derives directly fromFrameworkElement
. Label, on the other hand, derives fromContentControl
. This means thatLabel
can:Template
property).Content
property).DataTemplate
to its content (via theContentTemplate
property).Do whatever else a
ContentControl
can do that aFrameworkElement
cannot.Label
text is grayed out when disabledLabel
supports access keysLabel
is much heavier thanTextBlock
Source
Some more interesting reads below
标签通常支持单行文本输出,而 TextBlock 用于多行文本显示。
例如,在wpf中,TextBlock有一个属性
TextWrapping
,它可以启用多行输入;标签上没有这个。Labels usually support single line text output while the TextBlock is intended for multiline text display.
For example in wpf TextBlock has a property
TextWrapping
which enables multiline input; Label does not have this.Label
是ContentControl
这意味着您可以将任何内容设置为它的内容。绝对是任何东西,包括字符串、数字、日期、其他控件、图像、形状等。TextBlock
只能处理字符串
。Label
isContentControl
which means that you can set anything as a content for it. Absolutely anything including strings, numbers, dates, other controls, images, shapes, etc.TextBlock
can handle onlystrings
.尽管 TextBlock 和 Label 都用于显示文本,但它们在本质上有很大不同。
=> Label 继承自 ContentControl,一个基类,
可以显示几乎任何可以想象到的用户界面。
=>另一方面,TextBlock 直接从 FrameworkElement 继承,因此错过了从 Control 继承的所有元素所共有的行为。
TextBlock 的浅继承层次结构使控件比 Label 更轻,更适合更简单的非交互式场景。
PS:但是,如果您希望访问键起作用或者想要更灵活或图形化的设计,则需要使用Label。
Although TextBlock and Label are both used to display text, they are quite different under the covers.
=> Label inherits from ContentControl, a base class that
enables the display of almost any UI imaginable.
=> TextBlock, on the other hand, inherits directly from FrameworkElement, thus missing out on the behavior that is common to all elements inheriting from Control.
The shallow inheritance hierarchy of TextBlock makes the control lighter weight than Label and better suited for simpler, noninteractive scenarios.
PS: However, if you want access keys to work or want a more flexible or graphical design, you’ll need to use Label.
也许
TextBlock
最烦人的功能是隐式样式查找行为,其范围仅限于最接近的DataTemplate
。这是非Control
xaml 元素的默认行为。产生的结果为:
您可以阅读更多相关信息此处。
Probably the most annoying feature of
TextBlock
is the implicit style lookup behavior, which is scoped to only to the closestDataTemplate
. This is a default behavior for nonControl
xaml elements.Yields a result of:
You can read more about it here.