Run vs. Content vs. Text in WPF

余生颓废 提交于 2020-07-28 08:58:47

问题


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?


回答1:


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.




回答2:


A short word on 'property elements'

This is just an additional remark on Xaviers great answer.

In fact there is not much difference between:

<TextBlock Text="something" />

and

<TextBlock><TextBlock.Text>something</TextBlock.Text></TextBlock>

Thats because the Text property is of type String which is a simple type which can be set directly like Text="something".

The latter syntax in the example above is called 'property elements'. It's usually used when setting the attribute of an element to a complex type.

Example:

<Button>
  <Button.Content>
     <Rectangle Height="20", Width="20", Fill="Black"/>
  </Button.Content>
</Button>

In this example, you could propably set the 'Rectangle' directly without using Button.Content, but for other examples the syntax can be used to set an attribute of a complex type.



来源:https://stackoverflow.com/questions/29316922/run-vs-content-vs-text-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!