Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
WPF: Textblock Vs Label
WhatsApp
Nipun Tomar
12y
102.3
k
0
1
25
Blog
In WPF both textblock and level are used to show a small amount of text means visually both snippets produce the same UI. But there is big difference between the two:
TEXTBLOCK:
TextBlock lives in the System.Windows.Controls namespace, it is not a control. It derives directly from FrameworkElement. TextBlocks are used in a lot of controls to display text.
LABEL:
Label, on the other hand, derives from ContentControl means that label can: be given a custom control template (viaTemplate property), Display data other than just a string (via Content property), Apply a DataTemplate to its content (via ContentTemplate property), and can do whatever else a ContentControl can do that a FrameworkElement cannot. It supports access keys.
Label vs TextBlock (class hierarchy)
CONCLUSION:
If you want to use styles in WPF correctly (and you need to modify the margin, etc), It is recommend to use a
Label
instead of a TextBlock. TextBlocks are used inside a lot of controls, and modifying the TextBlock style has a major impact on how most controls (such as a Button, ComboBox, GridView Columns, etc) behave.
So, if you want to display text as a control itself, always use
Label
. The benefits of showing styles correctly (and have more control over styles and themes) are better than the fact that a TextBlock is lighter.
Up Next
WPF Layout: Margin Vs Padding
Ebook Download
View all
WPF Simplified: Build Windows Apps Using C# and XAML
Read by 1.5k people
Download Now!
Learn
View all
Membership not found