如何:创建文本修饰

TextDecoration 对象是可以添加到文本的视觉装饰。 有四种类型的文本修饰:下划线、基线、删除线和上划线。 下面的示例演示了文本修饰相对于文本的位置。

文本修饰类型示例

文本修饰位置示意图

若要为文本添加文本修饰,请创建一个 TextDecoration 对象并修改其属性。 可以使用 Location 属性来指定文本修饰的显示位置(如下划线)。 可以使用 Pen 属性来指定文本修饰的外观(如实心或渐变颜色)。 如果您没有为 Pen 属性指定值,则在默认情况下文本修饰的颜色将与文本的颜色相同。 在定义了 TextDecoration 对象之后,请将它添加到所需文本对象的 TextDecorations 集合中。

下面的示例演示用线性渐变画笔和虚线钢笔设计的文本修饰。

用线性渐变画笔和虚线笔设计的下划线示例

采用线性渐变下划线的文本修饰

Hyperlink 对象是一个内联级别的流内容元素,允许您在流内容中承载超链接。 默认情况下,Hyperlink 使用 TextDecoration 对象来显示下划线。 实例化 TextDecoration 对象可能会大幅降低性能,使用了许多 Hyperlink 对象时尤其如此。 如果您使用大量 Hyperlink 元素,则可能需要考虑仅在触发某个事件(如 MouseEnter 事件)时才显示下划线。

在下面的示例中,“My MSN”(我的 MSN)的下划线是动态的,它仅在触发 MouseEnter 事件时才出现。

用 TextDecorations 定义的超链接

显示 TextDecoration 的超链接

有关更多信息,请参见如何:将文本修饰用于超链接

示例

在下面的代码示例中,下划线文本修饰使用默认的字体值。

        ' Use the default font values for the strikethrough text decoration.
        Private Sub SetDefaultStrikethrough()
            ' Set the underline decoration directly to the text block.
            TextBlock1.TextDecorations = TextDecorations.Strikethrough
        End Sub
// Use the default font values for the strikethrough text decoration.
private void SetDefaultStrikethrough()
{
    // Set the underline decoration directly to the text block.
    TextBlock1.TextDecorations = TextDecorations.Strikethrough;
}
<!-- Use the default font values for the strikethrough text decoration. -->
<TextBlock
  TextDecorations="Strikethrough"
  FontSize="36" >
  The quick red fox
</TextBlock>

下面的代码示例将使用钢笔的纯色画笔来创建下划线文本修饰。

        ' Use a Red pen for the underline text decoration.
        Private Sub SetRedUnderline()
            ' Create an underline text decoration. Default is underline.
            Dim myUnderline As New TextDecoration()

            ' Create a solid color brush pen for the text decoration.
            myUnderline.Pen = New Pen(Brushes.Red, 1)
            myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended

            ' Set the underline decoration to a TextDecorationCollection and add it to the text block.
            Dim myCollection As New TextDecorationCollection()
            myCollection.Add(myUnderline)
            TextBlock2.TextDecorations = myCollection
        End Sub
// Use a Red pen for the underline text decoration.
private void SetRedUnderline()
{
    // Create an underline text decoration. Default is underline.
    TextDecoration myUnderline = new TextDecoration();

    // Create a solid color brush pen for the text decoration.
    myUnderline.Pen = new Pen(Brushes.Red, 1);
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

    // Set the underline decoration to a TextDecorationCollection and add it to the text block.
    TextDecorationCollection myCollection = new TextDecorationCollection();
    myCollection.Add(myUnderline);
    TextBlock2.TextDecorations = myCollection;
}
<!-- Use a Red pen for the underline text decoration -->
<TextBlock
  FontSize="36" >
  jumped over
  <TextBlock.TextDecorations>
    <TextDecorationCollection>
      <TextDecoration 
        PenThicknessUnit="FontRecommended">
        <TextDecoration.Pen>
          <Pen Brush="Red" Thickness="1" />
        </TextDecoration.Pen>
      </TextDecoration>
    </TextDecorationCollection>
  </TextBlock.TextDecorations>
</TextBlock>

下面的代码示例使用虚线钢笔的线性渐变画笔创建一个下划线文本修饰。

        ' Use a linear gradient pen for the underline text decoration.
        Private Sub SetLinearGradientUnderline()
            ' Create an underline text decoration. Default is underline.
            Dim myUnderline As New TextDecoration()

            ' Create a linear gradient pen for the text decoration.
            Dim myPen As New Pen()
            myPen.Brush = New LinearGradientBrush(Colors.Yellow, Colors.Red, New Point(0, 0.5), New Point(1, 0.5))
            myPen.Brush.Opacity = 0.5
            myPen.Thickness = 1.5
            myPen.DashStyle = DashStyles.Dash
            myUnderline.Pen = myPen
            myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended

            ' Set the underline decoration to a TextDecorationCollection and add it to the text block.
            Dim myCollection As New TextDecorationCollection()
            myCollection.Add(myUnderline)
            TextBlock3.TextDecorations = myCollection
        End Sub
// Use a linear gradient pen for the underline text decoration.
private void SetLinearGradientUnderline()
{
    // Create an underline text decoration. Default is underline.
    TextDecoration myUnderline = new TextDecoration();

    // Create a linear gradient pen for the text decoration.
    Pen myPen = new Pen();
    myPen.Brush = new LinearGradientBrush(Colors.Yellow, Colors.Red, new Point(0, 0.5), new Point(1, 0.5));
    myPen.Brush.Opacity = 0.5;
    myPen.Thickness = 1.5;
    myPen.DashStyle = DashStyles.Dash;
    myUnderline.Pen = myPen;
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;

    // Set the underline decoration to a TextDecorationCollection and add it to the text block.
    TextDecorationCollection myCollection = new TextDecorationCollection();
    myCollection.Add(myUnderline);
    TextBlock3.TextDecorations = myCollection;
}
<!-- Use a linear gradient pen for the underline text decoration. -->
<TextBlock FontSize="36">the lazy brown dog.
  <TextBlock.TextDecorations>
    <TextDecorationCollection>
      <TextDecoration  
        PenThicknessUnit="FontRecommended">
        <TextDecoration.Pen>
          <Pen Thickness="1.5">
            <Pen.Brush>
              <LinearGradientBrush Opacity="0.5"
                StartPoint="0,0.5"  EndPoint="1,0.5">
                <LinearGradientBrush.GradientStops>
                  <GradientStop Color="Yellow" Offset="0" />
                  <GradientStop Color="Red" Offset="1" />
                </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
            </Pen.Brush>
            <Pen.DashStyle>
              <DashStyle Dashes="2"/>
            </Pen.DashStyle>
          </Pen>
        </TextDecoration.Pen>
      </TextDecoration>
    </TextDecorationCollection>
  </TextBlock.TextDecorations>
</TextBlock>

请参见

任务

如何:将文本修饰用于超链接

参考

TextDecoration

Hyperlink