2010-09-01 20 views
1

Je crée un éditeur de texte WPF à l'aide de TextFormatter. J'ai besoin de mettre en retrait certains paragraphes, donc j'utilise la propriété Indent de la classe TextParagraphProperties.Indentation dans WPF TextFormatter

Cela fonctionne très bien dans ce scénario:

texte ordinaire sans aucune indentation
.
        Ce paragraphe a un uniforme
      indentation   donc tout est
        ok.

Mais je dois aussi ceci:
        John: Ce paragraphe a une
                        indentation diferent
                        dans la première ligne.
        Joe: Je ne sais pas comment
                        pour y arriver.

J'ai trouvé les propriétés ParagraphIndent et FirstLineInParagraph, mais je ne sais pas comment elles fonctionnent, ou si elles seraient utiles.

Merci d'avance !! Jose

+0

Une chance? J'essaie de résoudre le même problème. Si vous avez eu du succès, ce serait bien si vous répondez à votre propre question! –

Répondre

1

Jose -

Espérons que ce squelette de code vous aidera ... Je suppose que vous avez commencé à partir du texte Mise en forme avancée projet sur MSDN, qui, est malheureusement pas très avancé. Voici un fragment de code que vous pouvez présenter MainWindow.Xaml.cs:

 TextParagraphProperties textParagraphProperties = new GenericTextParagraphProperties(TextAlignment.Left, 
                          false); 
     TextRunCache textRunCache = new TextRunCache(); 

     // By default, this is the first line of a paragrph 
     bool firstLine = true; 
     while (textStorePosition < _textStore.Length) 
     { 
      // Create a textline from the text store using the TextFormatter object. 
      TextLine myTextLine = formatter.FormatLine(
       _textStore, 
       textStorePosition, 
       96 * 6, 

       // ProxyTextParagraphProperties will return a different value for the Indent property, 
       // depending on whether firstLine is true or not. 
       new ProxyTextParagraphProperties(textParagraphProperties, firstLine), 
       lastLineBreak, 
       textRunCache); 

      // Draw the formatted text into the drawing context. 
      Debug.WriteLine("linePosition:" + linePosition); 
      myTextLine.Draw(dc, linePosition, InvertAxes.None); 

      // Update the index position in the text store. 
      textStorePosition += myTextLine.Length; 

      // Update the line position coordinate for the displayed line. 
      linePosition.Y += myTextLine.Height; 

      // Figure out if the next line is the first line of a paragraph 
      var textRunSpans = myTextLine.GetTextRunSpans(); 
      firstLine = (textRunSpans.Count > 0) && (textRunSpans[textRunSpans.Count - 1].Value is TextEndOfParagraph); 

      lastLineBreak = myTextLine.GetTextLineBreak(); 
     } 

Vous devez créer les ProxyTextParagraphProperties de classe, qui descend de TextParagraphProperties. Voici un petit coup de ce que j'ai fait pour tester ceci:

class ProxyTextParagraphProperties : TextParagraphProperties 
{ 
    private readonly TextParagraphProperties _paragraphProperties; 
    private readonly bool _firstLineInParagraph; 

    public ProxyTextParagraphProperties(TextParagraphProperties textParagraphProperties, bool firstLineInParagraph) 
    { 
     _paragraphProperties = textParagraphProperties; 
     _firstLineInParagraph = firstLineInParagraph; 
    } 

    public override FlowDirection FlowDirection 
    { 
     get { return _paragraphProperties.FlowDirection; } 
    } 

    // implement the same as above for all the other properties... 

    // But we need to handle this one specially... 
    public override double Indent 
    { 
     get 
     { 
      return _firstLineInParagraph ? _paragraphProperties.Indent : 0; 
     } 
    } 
}