Difference between Label and TextBlock

78,460

Solution 1

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, 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.

    • Label text is grayed out when disabled
    • Label supports access keys
    • Label is much heavier than TextBlock

Source

Some more interesting reads below

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

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

Share:
78,460

Related videos on Youtube

Rami Alshareef
Author by

Rami Alshareef

Experienced and enthusiast Full Stack Software Engineer with passion toward best practices, collaborative work, Agile culture, clean code, and software architecture. Experienced with all stages of software development including QA and DevOps. I enjoy aligning business needs with software engineering and always seek to expand my engineering area of expertise and influence . Forward thinker and strategic planner, with aptitude for learning new skills. Team oriented with hands on scrum values and practices. It is a true privilege and beyond rewarding to work at a place that influences thousands of developers around the world. Rami's experiences include but are not limited to: • Scrum, Agile framework, and collaborative work • Restful &amp; web api, Solid OOP, DevOps • Software architecture, design, development, deployment process, and QA • Research and implementation • Database architecture and design

Updated on April 29, 2020

Comments

  • Rami Alshareef
    Rami Alshareef about 4 years

    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?

    • vortexwolf
      vortexwolf about 13 years
      In Silverlight the Label control changes itself after validation (the text becames red). But WPF is different.
  • Jim Balter
    Jim Balter almost 8 years
    "other controls" includes a TextBlock, so a Label can contain wrapped text, for instance.
  • Jim Balter
    Jim Balter almost 8 years
    Label has an arbitrary Content property. That can be a string, but it can also be a TextBlock (or any other control), so Labels can be multiline by using a TextBlock as the content.
  • oddRaven
    oddRaven about 7 years
    On your second point: it is possible to bind a DateTime to a TextBlock.