Чем отличается textbox от textblock
Перейти к содержимому

Чем отличается textbox от textblock

  • автор:

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

@Rachel Why is this question marked as a duplicate of one that was written 3 years later (especially given that this one has more upvotes and better answers)?

Jun 16, 2015 at 3:03

6 Answers 6

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.

answered Sep 12, 2008 at 17:58
8,763 7 7 gold badges 36 36 silver badges 31 31 bronze badges

@Kugel: I see no reference to Label in the linked page, so I can’t see how that is a justification for a TextBlock being slower than a Label . Can you please elaborate? If a Label uses a TextBlock for rendering, as joshsmithonwpf.wordpress.com/2007/07/04/… suggests, what makes a TextBlock slower?

TextBox input to TextBlock in other page

I’m using c# and WPF web application and I want to build a simple form which contains two text boxes for user input (name and phone number) and a «send» button. when the user clicks on the send button it will redirect to an other page and will display (with textblock) the entered values. I tried to read about data binding, but still didn’t success to make it work.
1. How do I save the entered values into a variable ?
2. How do I call these variables from the second page and display the saved text ?
Hope for help, thanks! xaml code of the form:

enter details: Name: Phone:     

what should be the code behind ? i simply want to display the entered values on an other page with textblock. this is the function i use to redirect to the «result page»:

public void sendButton_Click(object sender, RoutedEventArgs e) < Submit(); >void Submit()

Ok I succeed to make it work thanks to Mike’s answer. Thanks Mike, for other users this how the code looks like now:
form page xaml code:

enter details: Name: Phone:     

form page c# behind code:

 public partial class Page1 : Page < public Page1() < InitializeComponent(); >public void sendButton_Click(object sender, RoutedEventArgs e) < Submit(); >void Submit() < Page2 resultpage = new Page2(getName.Text, getPhoneNumber.Text); NavigationService.Navigate(resultpage); >> 

result page xaml code:
result page c# code behind:

 public Page2(string name, string phoneNumber)

Difference between Label and TextBlock

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?

16.8k 26 26 gold badges 64 64 silver badges 92 92 bronze badges
asked Mar 21, 2011 at 19:59
Rami Alshareef Rami Alshareef
7,110 13 13 gold badges 48 48 silver badges 77 77 bronze badges

In Silverlight the Label control changes itself after validation (the text becames red). But WPF is different.

Mar 21, 2011 at 20:08

5 Answers 5

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

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?

180k 47 47 gold badges 342 342 silver badges 506 506 bronze badges
answered Mar 22, 2011 at 4:38
17.8k 10 10 gold badges 61 61 silver badges 96 96 bronze badges
On your second point: it is possible to bind a DateTime to a TextBlock.
Feb 28, 2017 at 13:22
I take it TextBlock is more performant, correct me if I’m wrong.
Jan 1, 2023 at 14:04

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.

answered Mar 21, 2011 at 20:03
rene_buehling rene_buehling
724 8 8 silver badges 19 19 bronze badges

Label has an arbitrary Content property. That can be a string, but it can also be a TextBlock (or any other control), so Labels can be multiline by using a TextBlock as the content.

May 18, 2016 at 23:39

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 .

answered Mar 21, 2011 at 20:08
17.1k 3 3 gold badges 44 44 silver badges 67 67 bronze badges
«other controls» includes a TextBlock, so a Label can contain wrapped text, for instance.
May 18, 2016 at 23:38

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.

WPF TextBox and TextBlock

In a WPF application, what are the similarities and differences between the textbox and textblock controls? The answer was posted on StackOverflow.com.

Common to both TextBlocks and TextBoxes:

  • Can be used to display text
  • Can be set to specific Height and Width or be set to Auto so that they grow in size with the text.
  • Can set font size, font type, font styling, to wrap and to range left, right or centred.
  • Can have opacity set and have Pixel Shaders applied.
  • Used for displaying text more focused typographically.
  • Can contain text set to different colors, fonts and sizes.
  • The line height can also be increased from the default setting to give more space between each line of text.
  • Text inside a TextBlock cannot be made selectable by the user.
  • Used for displaying text more focused for content input or when content is needed to be made selectable by the user.
  • Can only be set to one colour, one font size, one font type etc.
  • Have fixed Line Spacing.
  • Can also be set to a fixed height and width but also have scrollbars switched on to allow content to expand.

The Label is similar to the TextBlock in that you can use it to put read-only text on the screen. Generally, a Label is for short, one-line texts (but may include an image), while the TextBlock works well for multi-line strings, but can only contain text (strings).

Search Entire Site

Recent Posts

  • Line Charts in Tableau
  • Shared Axis Bar Chart
  • Word Section Headers
  • Microsoft Word Columns
  • Bar Chart with Hierarchy
  • Stacked Bar Chart in Tableau
  • Bar Charts in Tableau
  • Getting Started with Tableau
  • The Filters Shelf in Tableau
  • Shelves and Cards in Tableau
  • Calculated Fields in Tableau
  • Tableau’s Dimensions and Measures
  • Tableau Public Desktop
  • Deep Learning Introduction

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *