上章我们学习了文本框最核心的功能:显示字符串,在必要的时候换行。我们还用了其他颜色来凸显文字,如果你想做的远远不止这些,怎么办?
幸好文本框支持内联的内容。这些像控件一样的结构全部继承于内联类,这意外着它们可以作为文本的一部分来传递。支持的元素包括AnchoredBlock, Bold, Hyperlink, InlineUIContainer, Italic, LineBreak, Run, Span, and Underline。接下来我们一个一个来看。
Bold, Italic and 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元素的子类,各自实现了具体的属性值来达到预期的效果。如Bold标签设置了FontWeight属性,Italic设置了FontStyle属性。
LineBreak
插入一个换行符。可以参看前面的例子,我们用过。
Hyperlink
超链接用于把链接添加到文本中。它的显示样式由系统主题决定,通常是蓝色的字体带下划线,鼠标悬停时变小手同时字体变红色。通过使用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内部的网页,可以在网页之间跳转。这种情况下,你不需要具体处理RequestNavigate事件。但是,如果是从WPF程序加载外部URL,这个事件非常有用。我们订阅RequestNavigate事件后,在加载URL时可以带一个事件处理函数:
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
}
允许你使用所有Span元素可用的属性来设置文本的样式。但是Span元素可能包含其他内联元素,因此Run元素可能只包含空文本。这使得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元素是最好的原则。
从后台代码来格式化文本
从XAML格式文本非常容易,但是有时候,你会遇到需要在后台代码里实现。这个有点麻烦,来看例子:
<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的伟大。