Difference between Label and TextBlock

前端 未结 5 2007
小鲜肉
小鲜肉 2020-11-30 19:40

According to the Windows Applications Development with Microsoft .NET 4 70-511 Training Kit

What is the difference between the Label control and T

相关标签:
5条回答
  • 2020-11-30 20:12

    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

    • http://www.wpfwiki.com/WPF%20Q4.1.ashx
    • What is the difference between the WPF TextBlock element and Label control?
    0 讨论(0)
  • 2020-11-30 20:12

    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.

    0 讨论(0)
  • 2020-11-30 20:30

    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:

    You can read more about it here.

    0 讨论(0)
  • 2020-11-30 20:36

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题