TOC

This article has been localized into Chinese by the community.

基本控制項:

TextBlock控件 - 内联格式

在前一篇文章中我们关注了TextBlock控件的核心功能:显示一个简单的字符串并在有需要的时候换行,我们甚至使用了预设以外的其他颜色来呈现文字,但如果你想要不仅仅只是对于所有TextBlock内的文字定义一个静态颜色呢?

幸运的是,TextBlock控件支持内联内容。 这些类似控件的小构造都继承自Inline类,这意味着它们可以作为较大文本的一部分进行内联呈现。 在撰写时,支持的元素包括AnchoredBlock,Bold,Hyperlink,InlineUIContainer,Italic,LineBreak,Run,Span和Underline。 在以下示例中,我们将大致了解它们。

粗體(Bold), 斜體(Italic), 與底線(Underline)

这些可能是最简单的内联元素类型。 这些名字应该告诉你很多关于它们的作用,但我们仍然会给你一个关于如何使用它们的简单例子:

<Window x:Class="WpfTutorialSamples.Basic_controls.TextBlockInlineSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlockInlineSample" Height="100" Width="300">
    <Grid>
		<TextBlock Margin="10" TextWrapping="Wrap">
			TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
		</TextBlock>
    </Grid>
</Window>

和HTML语言很相似,你只需将文字写在Bold标签内部,就可以使其粗体显示。斜体和下划线也类似。这一特性让你可以在你的应用中创建并显示风格多变的文字。

这三个标签只是Span元素的子类。他们各自设置了Span元素的一个属性,以显示所需的效果。例如,Bold标签设置了FontWeight属性,而Italic标签设置了FontStyle属性。

斷行LineBreak

簡單的在文本中插入換行符即可。有關我們使用LineBreak元素的範例,請參閱之前的章節。

超連結 (Hyperlink)

超連結(Hyperlink)元素允許您在文本中包含連結。 它使用適合您當前Windows主題的樣式進行渲染,該主題通常是帶下引線的藍色文本,並帶有滑鼠的手形指標和紅色懸停效果。 您可以使用NavigateUri屬性來定義要導航的URL。 這是一個例子:

<Window x:Class="WpfTutorialSamples.Basic_controls.TextBlockHyperlinkSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlockHyperlinkSample" Height="100" Width="300">
	<Grid>
		<TextBlock Margin="10" TextWrapping="Wrap">
			This text has a <Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="https://www.google.com">link</Hyperlink> in it.
		</TextBlock>
	</Grid>
</Window>

超链接也用于 WPF page 的内部,在 WPF 中,超链接可用于在 page 之间切换。在这种情况下,您不必像示例中那样专门处理 RequestNavigate 事件;但如果要从常规 WPF 应用程序启动外部 URL ,我们仍需要此事件和 Process 类提供一些帮助。我们注册 RequestNavigate 事件,它允许我们使用后置代码文件中的一个简单事件处理方法在用户默认浏览器中启动链接的 URL ,比如下面这段代码:

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
	System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
}

Run

Run元素允许你使用所有可以在Span元素中使用的属性来定义文本的样式。但相对于Span可以包含其他行内元素,Run只能包含纯文本。比较起来很显然Span元素更灵活而且在绝大多数情况上都是一个更合理的选择

Span

Span元素本身并没有任何默认的显示效果,但允许你设置几乎所有的显示效果,包括字体大小、字体样式和粗细,以及背景和前景颜色等等。Span元素最伟大的地方在于它能包含其他行内元素在其中,使得构建更为复杂的文本以及样式非常容易。在接下来的例子当中,我给大家展示了一些Span元素能做到的事情:

<Window x:Class="WpfTutorialSamples.Basic_controls.TextBlockSpanSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlockSpanSample" Height="100" Width="300">
    <Grid>
		<TextBlock Margin="10" TextWrapping="Wrap">
			This <Span FontWeight="Bold">is</Span> a
			<Span Background="Silver" Foreground="Maroon">TextBlock</Span>
			with <Span TextDecorations="Underline">several</Span>
			<Span FontStyle="Italic">Span</Span> elements,
			<Span Foreground="Blue">
				using a <Bold>variety</Bold> of <Italic>styles</Italic>
			</Span>.
		</TextBlock>
	</Grid>
</Window>

如您所見,如果沒有其他元素可以更符合您的需求,或者您只想要一個空白畫布來開始格式化文本,Span元素是一個很好的選擇。

从 C# 或逻辑代码格式化文本

如您所見,使用XAML格式化文本非常簡單,但在某些情況下,您可能更喜歡甚至需要從C#後置程式碼的檔案中執行此操作。 這有點麻煩,但這裡有一個關於如何做到這一點的例子:

<Window x:Class="WpfTutorialSamples.Basic_controls.TextBlockCodeBehindSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBlockCodeBehindSample" Height="100" Width="300">
    <Grid></Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfTutorialSamples.Basic_controls
{
	public partial class TextBlockCodeBehindSample : Window
	{
		public TextBlockCodeBehindSample()
		{
			InitializeComponent();
			TextBlock tb = new TextBlock();
			tb.TextWrapping = TextWrapping.Wrap;
			tb.Margin = new Thickness(10);
			tb.Inlines.Add("An example on ");
			tb.Inlines.Add(new Run("the TextBlock control ") { FontWeight = FontWeights.Bold });
			tb.Inlines.Add("using ");
			tb.Inlines.Add(new Run("inline ") { FontStyle = FontStyles.Italic });
			tb.Inlines.Add(new Run("text formatting ") { Foreground = Brushes.Blue });
			tb.Inlines.Add("from ");
			tb.Inlines.Add(new Run("Code-Behind") { TextDecorations = TextDecorations.Underline });
			tb.Inlines.Add(".");
			this.Content = tb;
		}
	}
}

有這種可能性很好,在某些情況下可能必需要這樣做,但這個例子可能會讓你更加欣賞XAML。


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!