我需要突出显示AvalonEdit中所有出现的所选单词。我创建了一个HihglinghtingRule类的实例:

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

之后该怎么办?
谢谢。

最佳答案

要使用该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
        }
    }
}

关于c# - 突出显示AvalonEdit中所有出现的所选单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9223674/

10-13 08:39