Megosztás a következőn keresztül:


Útmutató: Szövegdekoráció létrehozása

A TextDecoration objektum egy vizuális díszítőelem, amelyet hozzáadhat a szöveghez. A szövegdekorációknak négy típusa van: aláhúzás, alapvonal, áthúzás és felső vonal. Az alábbi példa a szövegdekorációk szöveghez viszonyított helyét mutatja be.

Szövegdekorációs típusok diagramja

Ha szövegdekorációt szeretne hozzáadni a szöveghez, hozzon létre egy TextDecoration objektumot, és módosítsa annak tulajdonságait. A Location tulajdonság használatával megadhatja, hogy hol jelenik meg a szövegdekoráció, például aláhúzás. A Pen tulajdonság használatával megadhatja a szövegdekoráció megjelenését, például egyszínű kitöltést vagy színátmenetet. Ha nem ad meg értéket a Pen tulajdonsághoz, a dekorációk alapértelmezés szerint a szöveg színével megegyező színűek lesznek. Miután definiált egy TextDecoration objektumot, adja hozzá a kívánt szövegobjektum TextDecorations gyűjteményéhez.

Az alábbi példa egy lineáris színátmenetes ecsettel és szaggatott tollal formázott szövegdekorációt mutat be.

Lineáris színátmenetes aláhúzású szövegdekoráció

A Hyperlink objektum egy beágyazott szintű folyamattartalom-elem, amely lehetővé teszi, hogy hivatkozásokat tároljon a folyamat tartalmán belül. Alapértelmezés szerint Hyperlink egy TextDecoration objektumot használ az aláhúzás megjelenítéséhez. TextDecoration objektumok példányosítása teljesítményigényes lehet, különösen akkor, ha sok Hyperlink objektummal rendelkezik. Ha széles körben használja Hyperlink elemeket, érdemes lehet csak aláhúzást megjeleníteni egy esemény, például az MouseEnter esemény aktiválásakor.

A következő példában a "Saját MSN" hivatkozás aláhúzása dinamikus – csak akkor jelenik meg, ha a MouseEnter esemény aktiválódik.

Hivatkozások, amelyek szövegdekorációkat jelenítenek meg

További információért lásd: Határozza meg, hogy alá van-e húzva a hivatkozás.

példa

Az alábbi példakódban az aláhúzásos szövegdekoráció az alapértelmezett betűtípusértéket használja.

// 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.
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. -->
<TextBlock
  TextDecorations="Strikethrough"
  FontSize="36" >
  The quick red fox
</TextBlock>

A következő kód példában egy aláhúzásos szövegdekoráció jön létre a tollhoz tartozó egyszínű ecsettel.

// 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.
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 -->
<TextBlock
  FontSize="36" >
  jumps over
  <TextBlock.TextDecorations>
    <TextDecorationCollection>
      <TextDecoration 
        PenThicknessUnit="FontRecommended">
        <TextDecoration.Pen>
          <Pen Brush="Red" Thickness="1" />
        </TextDecoration.Pen>
      </TextDecoration>
    </TextDecorationCollection>
  </TextBlock.TextDecorations>
</TextBlock>

A következő kód példában egy aláhúzásos szövegdekoráció jön létre a szaggatott tollhoz tartozó lineáris színátmenetes ecsettel.

// 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.
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. -->
<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>

Lásd még