H. Pauwelyn H. Pauwelyn - 28 days ago 6
C# Question

Run vs. Content vs. Text in WPF

In a WPF (or even a Windows 8 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
?

Answer

Controls 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 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 define additional elements inside the setter.