wpf Label 和 TextBlock 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5382925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Label and TextBlock
提问by Rami Shareef
According to the Windows Applications Development with Microsoft .NET 4 70-511Training Kit
根据Windows Applications Development with Microsoft .NET 4 70-511Training Kit
What is the difference between the Label
control and TextBlock
control since both are content controls and just displaying text?
Label
control 和TextBlock
control之间有什么区别,因为两者都是内容控件并且只显示文本?
回答by biju
TextBlock is not a control
TextBlock 不是控件
Even though TextBlock
lives in the System.Windows.Controls namespace, it is not a control. It derives directly from FrameworkElement
. Label, on the other hand, derives from ContentControl
. This means that Label
can:
即使TextBlock
位于 System.Windows.Controls 命名空间中,它也不是控件。它直接来自FrameworkElement
. 另一方面,标签源自ContentControl
。这意味着Label
可以:
- 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 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
- 获得自定义控件模板(通过
Template
属性)。 - 显示除字符串以外的数据(通过
Content
属性)。 - 将 a
DataTemplate
应用于其内容(通过ContentTemplate
属性)。 做其他
ContentControl
能做FrameworkElement
而不能做的事。Label
禁用时文本显示为灰色Label
支持访问密钥Label
比TextBlock
Some more interesting reads below
下面还有一些更有趣的读物
回答by Snowbear
Label
is ContentControl
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 only strings
.
Label
是ContentControl
,这意味着你可以设置任何东西作为它的内容。绝对任何包括字符串、数字、日期、其他控件、图像、形状等的东西TextBlock
都只能处理strings
.
回答by rene_buehling
Labels usually support single line text output while the TextBlock is intended for multiline text display.
标签通常支持单行文本输出,而 TextBlock 用于多行文本显示。
For example in wpf TextBlock has a property TextWrapping
which enables multiline input; Label does not have this.
例如在 wpf TextBlock 中有一个TextWrapping
启用多行输入的属性;标签没有这个。
回答by Vinodhini Ramasamy
Although TextBlock and Label are both used to display text, they are quite different under the covers.
尽管 TextBlock 和 Label 都用于显示文本,但它们在幕后却大不相同。
=> Labelinherits from ContentControl, a base class that enables the display of almost any UI imaginable.
=> Label继承自ContentControl,这是一个基类,可以显示几乎所有可以想象的 UI。
=> 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.
=> TextBlock,另一方面,直接从FrameworkElement继承,因此错过了所有从 Control 继承的元素共有的行为。TextBlock 的浅层继承层次使得控件比 Label 更轻,更适合更简单的非交互场景。
PS: However, if you want access keysto work or want a more flexible or graphical design, you'll need to use Label.
PS:但是,如果您想让访问键起作用或者想要更灵活或图形化的设计,则需要使用 Label。
回答by Mateusz My?lak
Probably the most annoying feature of TextBlock
is the implicit style lookup behavior, which is scoped to only to the closest DataTemplate
. This is a default behavior for non Control
xaml elements.
可能最烦人的特性TextBlock
是隐式样式查找行为,它的范围仅限于最接近的DataTemplate
. 这是非Control
xaml 元素的默认行为。
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Red"/>
</Style>
</StackPanel.Resources>
<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</StackPanel>
Yields a result of:
产生以下结果:
You can read more about it here.
您可以在此处阅读更多相关信息。