2012-02-10 21 views
7

AvalonEditで選択した単語のすべての出現を強調表示する必要があります。私はHihglinghtingRuleクラスのインスタンスを作成しました:AvalonEditで選択した単語のすべての出現を強調表示します。

var rule = new HighlightingRule() 
    { 
     Regex = regex, //some regex for finding occurences 
     Color = new HighlightingColor {Background = new SimpleHighlightingBrush(Colors.Red)} 
    }; 

後で何をすればいいですか?おかげさまで

答えて

7

HighlightingRuleは、あなたが(などHighlightingColorizer)ハイライトエンジンの別のインスタンスを作成しなければならないことを使用するには

それはあなたの言葉浮き彫りにDocumentColorizingTransformer書くことがより簡単で効率的です:*

public class ColorizeAvalonEdit : DocumentColorizingTransformer 
{ 
    protected override void ColorizeLine(DocumentLine line) 
    { 
     int lineStartOffset = line.Offset; 
     string text = CurrentContext.Document.GetText(line); 
     int start = 0; 
     int index; 
     while ((index = text.IndexOf("AvalonEdit", start)) >= 0) { 
      base.ChangeLinePart(
       lineStartOffset + index, // startOffset 
       lineStartOffset + index + 10, // endOffset 
       (VisualLineElement element) => { 
        // This lambda gets called once for every VisualLineElement 
        // between the specified offsets. 
        Typeface tf = element.TextRunProperties.Typeface; 
        // Replace the typeface with a modified version of 
        // the same typeface 
        element.TextRunProperties.SetTypeface(new Typeface(
         tf.FontFamily, 
         FontStyles.Italic, 
         FontWeights.Bold, 
         tf.Stretch 
        )); 
       }); 
      start = index + 1; // search for next occurrence 
     } 
    } 
} 
+0

をありがとう!* –

+0

これがどのように質問に答えるかわかりません。ユーザーは、一致した場合にすべての単語がテキスト内でハイライト表示されるような動作を求めていました。ビジュアルスタジオのようなものがあります。 – Devid

関連する問題