运行 与 WPF 中的内容与文本

Run vs. Content vs. Text in WPF

在 WPF(甚至 Windows 8 或 8.1 应用程序)中,您可以通过三种可能的方式在控件中添加文本。

  1. Run元素里面TextBlock元素

    <TextBlock>
        <Run>My text</Run>
    </TextBlock>
    
  2. Text 属性 作为 TextBlock 元素的属性

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

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

这三种方法有什么区别?为什么必须对 TextBlock 使用 Text 而对 ComboboxItem 使用 Content

带有 Text 属性 的控件只能接受字符串,并以该控件处理的特定方式呈现。此类控件的示例有 TextBlock and TextBox.

带有 Content 属性 的控件实际上可以将任何 object 设置为那个 属性。这些控件通常将值转发给 Content property on a ContentPresenter. The ContentPresenter Class 文档具有以下相关块:

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.

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可以进本合集。

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.

至于<TextBlock Text="something" /><TextBlock><TextBlock.Text>something</TextBlock.Text></TextBlock>之间的区别,实际上没有区别。这些只是两种不同的方式,您可以通过它们设置 xaml 文件中某些内容的属性。第二个版本通常仅在需要在 setter.

中定义其他元素时使用

关于'property elements'

的一句话

这只是对 Xaviers 出色回答的补充评论。

其实区别不大:

<TextBlock Text="something" />

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

那是因为 Text 属性 是 String 类型,这是一个简单的类型,可以像 Text="something".

一样直接设置

上例中的后一种语法称为'property elements'。 通常在将元素的属性设置为复杂类型时使用。

示例:

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

在此示例中,您可以直接设置 'Rectangle' 而无需使用 Button.Content,但对于其他示例,语法可用于设置复杂类型的属性。