问题
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?
回答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:
- Be given a custom control template (via the
Templateproperty). - Display data other than just a string (via the
Contentproperty). - Apply a
DataTemplateto its content (via theContentTemplateproperty). Do whatever else a
ContentControlcan do that aFrameworkElementcannot.Labeltext is grayed out when disabledLabelsupports access keysLabelis much heavier thanTextBlock
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?
回答2:
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.
回答3:
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.
回答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.
来源:https://stackoverflow.com/questions/5382925/difference-between-label-and-textblock