WPF教程之 TextBlock控件 – 内联格式

基础控件:

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>

WPF教程之 TextBlock控件 - 内联格式

和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教程之 TextBlock控件 - 内联格式

超连结也可以使用 WPF 内部连结来实现 page 之间的切换。在例子中,你不需明确的 handle RequestNavigate 事件,而是启动标准 WPF 应用程式的内部连结,我们需要透过这个事件和 Process class得到一点帮助。我们注册 RequestNavigate 事件,它允许我们用一个简单的事件驱动来启动一个使用者预设浏览器的连结网址,像是下面这个程式码:

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>

WPF教程之 TextBlock控件 - 内联格式

如您所见,如果没有其他元素可以更符合您的需求,或者您只想要一个空白画布来开始格式化文本,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;
		}
	}
}

WPF教程之 TextBlock控件 - 内联格式

有这种可能性很好,在某些情况下可能必需要这样做,但这个例子可能会让你更加欣赏XAML。

作者:admin,如若转载,请注明出处:https://www.web176.com/wpf/16050.html

(0)
打赏 支付宝 支付宝 微信 微信
adminadmin
上一篇 2023年4月19日
下一篇 2023年4月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注