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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:41:19  来源:igfitidea点击:

Difference between Label and TextBlock

wpflabeltextblock

提问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 Labelcontrol and TextBlockcontrol since both are content controls and just displaying text?

Labelcontrol 和TextBlockcontrol之间有什么区别,因为两者都是内容控件并且只显示文本?

回答by biju

TextBlock is not a control

TextBlock 不是控件

Even though TextBlocklives 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 Labelcan:

即使TextBlock位于 System.Windows.Controls 命名空间中,它也不是控件。它直接来自FrameworkElement. 另一方面,标签源自ContentControl。这意味着Label可以:

  1. Be given a custom control template (via the Templateproperty).
  2. Display data other than just a string (via the Contentproperty).
  3. Apply a DataTemplateto its content (via the ContentTemplateproperty).
  4. Do whatever else a ContentControlcan do that a FrameworkElementcannot.

    • Labeltext is grayed out when disabled
    • Labelsupports access keys
    • Labelis much heavier than TextBlock
  1. 获得自定义控件模板(通过Template属性)。
  2. 显示除字符串以外的数据(通过Content属性)。
  3. 将 aDataTemplate应用于其内容(通过ContentTemplate属性)。
  4. 做其他ContentControl能做FrameworkElement而不能做的事。

    • Label禁用时文本显示为灰色
    • Label支持访问密钥
    • LabelTextBlock

Source

来源

Some more interesting reads below

下面还有一些更有趣的读物

回答by Snowbear

Labelis ContentControlwhich means that you can set anything as a content for it. Absolutely anything including strings, numbers, dates, other controls, images, shapes, etc. TextBlockcan handle only strings.

LabelContentControl,这意味着你可以设置任何东西作为它的内容。绝对任何包括字符串、数字、日期、其他控件、图像、形状等的东西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 TextWrappingwhich 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 TextBlockis the implicit style lookup behavior, which is scoped to only to the closest DataTemplate. This is a default behavior for non Controlxaml elements.

可能最烦人的特性TextBlock是隐式样式查找行为,它的范围仅限于最接近的DataTemplate. 这是非Controlxaml 元素的默认行为。

<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:

产生以下结果:

enter image description here

在此处输入图片说明

You can read more about it here.

您可以在此处阅读更多相关信息。