Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run vs. Content vs. Text in WPF

Tags:

In a WPF (or even a Windows 8 or 8.1 application) you have three possible ways to add a text in a control.

  1. Run element inside TextBlock element

    <TextBlock>     <Run>My text</Run> </TextBlock> 
  2. Text property as attribute of TextBlock element

    <TextBlock Text="My text" /> 
  3. Text property as element inside TextBlock element

    <TextBlock>     <TextBlock.Text>my text</TextBlock.Text> </TextBlock>` 

What are the differences between these three approaches? And why you must use Text for a TextBlock and Content for a ComboboxItem?

like image 436
H. Pauwelyn Avatar asked Mar 28 '15 12:03

H. Pauwelyn


People also ask

What is run in WPF?

Run is called to start a WPF application. If you define your Application using markup, or markup and code-behind, Run will be called implicitly. However, if you define your Application using code, you will need to explicitly call Run.

What is the difference between label and TextBlock in WPF?

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. Label has an arbitrary Content property.

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 content control in WPF?

Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.


1 Answers

A control with a Text property can only accept a string and is rendered in a specific way handled by that control. Examples of such controls are TextBlock and TextBox.

Controls with a Content property can have literally any object set to that property. These controls generally forward the value to the Content property on a ContentPresenter. The ContentPresenter Class documentation has this relevant block:

The ContentPresenter uses the following logic to display the Content:

  • If the ContentTemplate property on the ContentPresenter is set, the ContentPresenter applies that DataTemplate to the Content property and the resulting UIElement and its child elements, if any, are displayed. For more information about DataTemplate objects, see Data Templating Overview.
  • If the ContentTemplateSelector property on the ContentPresenter is set, the ContentPresenter applies the appropriate DataTemplate to the Content property and the resulting UIElement and its child elements, if any, are displayed.
  • If there is a DataTemplate associated with the type of Content, the ContentPresenter applies that DataTemplate to the Content property and the resulting UIElement and its child elements, if any, are displayed.
  • If Content is a UIElement object, the UIElement is displayed. If the UIElement already has a parent, an exception occurs.
  • If there is a TypeConverter that converts the type of Content to a UIElement, the ContentPresenter uses that TypeConverter and the resulting UIElement is displayed.
  • If there is a TypeConverter that converts the type of Content to a string, the ContentPresenter uses that TypeConverter and creates a TextBlock to contain that string. The TextBlock is displayed.
  • If the content is an XmlElement, the value of the InnerText property is displayed in a TextBlock.
  • The ContentPresenter calls the ToString method on the Content and creates a TextBlock to contain the string returned by ToString. The TextBlock is displayed.

In the case of the TextBlock class, you have the option to either set the Text property, or set the Inlines property. Setting Text will simply render the text. Setting Inlines (which is the default if you put content inside the body of the xaml tag) allows you to format your text. For example, you could use a Run with its FontWeight set to Bold to make a certain word or phrase bold within a sentence. You can use a LineBreak to insert a new line. You can even use an InlineUIContainer to insert custom UI elements in the text. Anything that derives from the Inline class can go in this collection.

TextBlock is intended for simple bits of formatted text. If you want even more powerful document style features, you can look into FlowDocument, which is used by controls such as RichTextBox, FlowDocumentScrollViewer and FlowDocumentReader.

As far as the difference between <TextBlock Text="something" /> and <TextBlock><TextBlock.Text>something</TextBlock.Text></TextBlock>, there actually isn't a difference. Those are simply two different ways by which you can set properties on something in a xaml file. The second version is usually used only when you need to define additional elements inside the setter.

like image 70
Xavier Avatar answered Sep 27 '22 19:09

Xavier