2012-02-10 29 views
7

Necesito resaltar todas las apariciones de palabras seleccionadas en AvalonEdit. Creé una instancia de la clase HihglinghtingRule:Resaltar todas las apariciones de palabras seleccionadas en AvalonEdit

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

¿Qué debo hacer después? Gracias.

Respuesta

7

Para utilizar ese HighlightingRule, que habría que crear otra instancia del motor de resaltado (HighlightingColorizer etc.)

Es más fácil y más eficiente para escribir una DocumentColorizingTransformer que pone de relieve su palabra:

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

* ¡Muchas gracias! * –

+0

No veo cómo responde esto a la pregunta. El usuario quería un comportamiento donde todas las palabras se destaquen en un texto si coinciden. Algo similar como el estudio visual. – Devid

Cuestiones relacionadas