In a WPF (or even a Windows 8 application) you have three possible ways to add a text in a control.
<TextBlock Text="My text" />
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.