Nasıl yapılır: Metin Süslemesi Oluşturma

Nesne, TextDecoration metne ek olarak eklilen görsel bir ifadedir. Dört tür metin süslemesi vardır: alt çizgi, temel, üzerinde çizgi ve çizgi. Aşağıdaki örnek, metine göre metin süslemelerinin konumlarını gösterir.

Metin süslemesi türleri diyagramı

Metne metin süslemesi eklemek için bir nesnesi TextDecoration oluşturun ve özelliklerini değiştirin. Metin Location süslemenin nerede görüntülendiğinden (örneğin, alt çizgi) belirtmek için özelliğini kullanın. Düz Pen dolgu veya gradyan rengi gibi metin süslemesi görünümünü belirtmek için özelliğini kullanın. özelliği için bir değer Pen belirtmezseniz, süslemeler varsayılan olarak metinle aynı renge sahip olur. Bir nesne TextDecoration tanımdan sonra, nesneyi istenen TextDecorations metin nesnesinin koleksiyonuna ekleyin.

Aşağıdaki örnek, doğrusal gradyan fırça ve kesikli kalem ile stillenmiş bir metin süslemesini gösterir.

Doğrusal gradyan alt çizgi ile metin süslemesi

nesnesi, Hyperlink akış içeriğinde köprü barındırmaya olanak sağlayan satır içi bir akış içeriği öğesidir. Varsayılan olarak, Hyperlink alt çizgi görüntülemek için bir nesnesi TextDecoration kullanır. TextDecoration nesneleri, özellikle de çok sayıda nesne varsa, örneği oluşturma performansı yoğun Hyperlink olabilir. Öğeleri kapsamlı olarak kullanıyorsanız, yalnızca olay gibi bir olayı tetiklerken bir alt çizgi Hyperlink göstermeyi göz önünde bulundurarak göz önünde bulundurarak. MouseEnter

Aşağıdaki örnekte, "MY MSN" bağlantısının alt çizgisi dinamiktir; yalnızca olay MouseEnter tetiklendiğinde görünür.

MetinDeğerleri görüntüleyen köprüler

Daha fazla bilgi için bkz. Köprü altı çizili olup olmadığını belirtme.

Örnek

Aşağıdaki kod örneğinde, bir alt çizgi metin süslemesi varsayılan yazı tipi değerini kullanır.

// 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şağıdaki kod örneğinde, kalem için düz renk fırça ile bir alt çizgi metin süslemesi oluşturulur.

// 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şağıdaki kod örneğinde, kesikli kalem için doğrusal gradyan fırça ile bir alt çizgi metin süslemesi oluşturulur.

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

Ayrıca bkz.