Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the WPF TextBlock element and Label control? [duplicate]

Tags:

wpf

Visually both of the following snippets produce the same UI. So why are there 2 controls..
Snippet1

<TextBlock>Name:</TextBlock> <TextBox Name="nameTextBox" /> 

Snippet2

<Label>Name:</Label> <TextBox Name="nameTextBox" /> 

(Well I am gonna answer this myself... thought this is a useful tidbit I learnt today from Programming WPF)

like image 427
Gishu Avatar asked Sep 12 '08 14:09

Gishu


People also ask

What is the difference between label and TextBlock in WPF?

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

What is the difference between TextBox and TextBlock in WPF?

Text inside a TextBlock can not be made selectable by the user. TextBoxes are used for displaying text more focused for content input or when content is needed to be made selectable by the user. The TextBox can only be set to one colour, one font size, one font type etc.

What is a TextBlock in WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.

What is label in WPF?

Advertisements. The Label class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls.


1 Answers

The WPF Textblock inherits from FrameworkElement instead of deriving from System.Windows.Control like the Label Control. This means that the Textblock is much more lightweight. The downside of using a textblock is no support for Access/Accerelator Keys and there is no link to other controls as target.

When you want to display text by itself use the TextBlock. The benefit is a light, performant way to display text.

When you want to associate text with another control like a TextBox use the Label control. The benefits are access keys and references to target control.

like image 184
Alan Le Avatar answered Oct 19 '22 20:10

Alan Le