c# – 突出显示AvalonEdit中所有选定单词的出现次数
发布时间:2020-12-15 08:09:19 所属栏目:百科 来源:网络整理
导读:我需要在AvalonEdit中突出显示所有选定单词.我创建了一个HihglinghtingRule类的实例: var rule = new HighlightingRule() { Regex = regex,//some regex for finding occurences Color = new HighlightingColor {Background = new SimpleHighlightingBrush(
我需要在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 } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容