我需要在 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/