In WPF both Textblock and Label are used to show a small amount of text means visually both snippets produce the same UI.
But if you always has this confusion which one to use (Label or TextBlock) when showing a readonly data in GUI? Then just try to show “Label_Vs_TextBlock” in UI you will get your answer.
Label will show it as “LabelVs_TextBlock” (Label.Content = "Label_Vs_TextBlock").
TextBlock will show it as “Label_Vs_TextBlock” (TextBlock.Text= "Label_Vs_TextBlock").
As you can see Label is taking the first Underscore as AccessKey & removing it. While TextBlock is showing it perfectly.
So what does this mean TextBlock has won the ultimate war for showing readOnly text on your UI.
Then you are wrong, think again ………………
There is a 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 you can do whatever else a ContentControl can do that a FrameworkElement cannot. It supports access keys.
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 you can do whatever else a ContentControl can do that a FrameworkElement cannot. It supports access keys.
Label vs TextBlock (class hierarchy)
Still, If the above problem of accessKey is a nightmare to you help yourself & use the below style
<Style x:Key="{x:Type Label}" TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
CONCLUSION:
If you want to use styles in WPF correctly (and you need to modify the margin, etc), It is recommended 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.

No comments:
Post a Comment