在 WPF 中运行与内容与文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29316922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run vs. Content vs. Text in WPF
提问by H. Pauwelyn
In a WPF (or even a Windows 8 or 8.1 application) you have three possible ways to add a text in a control.
在 WPF(甚至 Windows 8 或 8.1 应用程序)中,您可以通过三种可能的方式在控件中添加文本。
Runelement insideTextBlockelement<TextBlock> <Run>My text</Run> </TextBlock>Textproperty as attribute ofTextBlockelement<TextBlock Text="My text" />Textproperty as element insideTextBlockelement<TextBlock> <TextBlock.Text>my text</TextBlock.Text> </TextBlock>`
Run元素内TextBlock元素<TextBlock> <Run>My text</Run> </TextBlock>Text属性作为TextBlock元素的属性<TextBlock Text="My text" />Text属性作为元素内的TextBlock元素<TextBlock> <TextBlock.Text>my text</TextBlock.Text> </TextBlock>`
What are the differences between these three approaches? And why you must use Textfor a TextBlockand Contentfor a ComboboxItem?
这三种方法有什么区别?为什么必须使用Textfor aTextBlock和Contentfor a ComboboxItem?
回答by Xavier
A control with a Textproperty can only accept a string and is rendered in a specific way handled by that control. Examples of such controls are TextBlockand TextBox.
具有Text属性的控件只能接受一个字符串,并以该控件处理的特定方式呈现。此类控件的示例是TextBlock和TextBox。
Controls with a Contentproperty can have literally any objectset to that property. These controls generally forward the value to the Contentproperty on a ContentPresenter. The ContentPresenter Classdocumentation has this relevant block:
具有Content属性的控件实际上object可以对该属性进行任何设置。这些控件通常将值转发到ContentPresenter上的Content属性。该ContentPresenter类文档有相关的块:
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.
ContentPresenter 使用以下逻辑来显示内容:
- 如果设置了 ContentPresenter 上的 ContentTemplate 属性,则 ContentPresenter 将该 DataTemplate 应用于 Content 属性,并显示生成的 UIElement 及其子元素(如果有)。有关 DataTemplate 对象的更多信息,请参阅数据模板概述。
- 如果设置了 ContentPresenter 上的 ContentTemplateSelector 属性,则 ContentPresenter 将适当的 DataTemplate 应用于 Content 属性,并显示生成的 UIElement 及其子元素(如果有)。
- 如果存在与 Content 类型关联的 DataTemplate,则 ContentPresenter 将该 DataTemplate 应用于 Content 属性,并显示生成的 UIElement 及其子元素(如果有)。
- 如果 Content 是 UIElement 对象,则显示 UIElement。如果 UIElement 已有父元素,则会发生异常。
- 如果存在将内容类型转换为 UIElement 的 TypeConverter,则 ContentPresenter 使用该 TypeConverter 并显示生成的 UIElement。
- 如果存在将 Content 类型转换为字符串的 TypeConverter,则 ContentPresenter 使用该 TypeConverter 并创建一个 TextBlock 以包含该字符串。显示文本块。
- 如果内容是 XmlElement,则 InnerText 属性的值显示在 TextBlock 中。
- ContentPresenter 调用 Content 上的 ToString 方法并创建一个 TextBlock 来包含 ToString 返回的字符串。显示文本块。
In the case of the TextBlockclass, you have the option to either set the Textproperty, or set the Inlinesproperty. Setting Textwill 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 Runwith its FontWeightset to Boldto make a certain word or phrase bold within a sentence. You can use a LineBreakto insert a new line. You can even use an InlineUIContainerto insert custom UI elements in the text. Anything that derives from the Inlineclass can go in this collection.
对于TextBlock类,您可以选择设置Text属性或设置Inlines属性。设置Text将简单地呈现文本。设置Inlines(如果您将内容放在 xaml 标记的正文中,则这是默认设置)允许您设置文本格式。例如,您可以使用Run并将其FontWeight设置Bold为使句子中的某个单词或短语加粗。您可以使用LineBreak插入新行。您甚至可以使用InlineUIContainer在文本中插入自定义 UI 元素。从Inline类派生的任何东西都可以放在这个集合中。
TextBlockis 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, FlowDocumentScrollViewerand FlowDocumentReader.
TextBlock适用于格式化文本的简单位。如果您想要更强大的文档样式功能,您可以查看FlowDocument,它由RichTextBox、FlowDocumentScrollViewer和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.
至于<TextBlock Text="something" />和之间的区别<TextBlock><TextBlock.Text>something</TextBlock.Text></TextBlock>,实际上没有区别。这只是您可以在 xaml 文件中设置属性的两种不同方式。第二个版本通常仅在您需要在 setter 中定义其他元素时使用。
回答by Martin
A short word on 'property elements'
关于“属性元素”的简短词
This is just an additional remark on Xaviers great answer.
这只是对 Xaviers 很好的回答的补充说明。
In fact there is not much difference between:
其实两者没有太大区别:
<TextBlock Text="something" />
and
和
<TextBlock><TextBlock.Text>something</TextBlock.Text></TextBlock>
Thats because the Textproperty is of type Stringwhich is a simple type which can be set directly like Text="something".
那是因为该Text属性的类型String是一个简单类型,可以像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.
在此示例中,您可以不使用 直接设置“矩形” Button.Content,但对于其他示例,语法可用于设置复杂类型的属性。

