AvalonEdit WPF TextEditor (SharpDevelop):如何突出显示特定范围的文本?
令人难以置信的 AvalonEdit WPF TextEditor 控件似乎缺少一个重要的功能,或者至少我无法弄清楚。 给定偏移量和长度,使用 HighlightColor 突出显示 TextDocument 中的该部分。很简单,对吧?
显然不是。我有 RTFM,“语法突出显示”的文档让我更加困惑。 其他人在 SharpDevelop 论坛中提出了同样的问题,我恐怕我无法理解格伦沃尔德先生的回答。
这是我的尝试,使用 DocumentHighlighter 类(当然它不起作用):
textEditor1.Text = "1234567890";
HighlightingColor c = new HighlightingColor() { FontWeight = FontWeights.ExtraBold };
DocumentHighlighter dh = new DocumentHighlighter(textEditor1.Document, new HighlightingRuleSet());
HighlightedLine hl = dh.HighlightLine(1);
hl.Sections.Add(new HighlightedSection() { Color = c, Offset = 1, Length = 3 });
谢谢您的帮助!
The incredibly awesome AvalonEdit WPF TextEditor control seems to lack an important feature, or at least i can't figure it out. Given an offset and a length, highlight that portion in the TextDocument with a HighlightColor. Simple, right?
Apprentely not. I've RTFM, and the documentation on "Syntax Highlighting" confused me even more. Someone else asked the same question in the SharpDevelop forums, and i'm afraid i can't make sense of Herr Grunwald's answer.
Here's my attempt, using the DocumentHighlighter class (of course it doesn't work):
textEditor1.Text = "1234567890";
HighlightingColor c = new HighlightingColor() { FontWeight = FontWeights.ExtraBold };
DocumentHighlighter dh = new DocumentHighlighter(textEditor1.Document, new HighlightingRuleSet());
HighlightedLine hl = dh.HighlightLine(1);
hl.Sections.Add(new HighlightedSection() { Color = c, Offset = 1, Length = 3 });
Thank you for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否在这篇文章中看到了这一点 - 这似乎正是您所要求的:
它用粗体突出显示单词 AvalonEdit。
Did you see this in this article - it seems to be exactly what are you asking for:
It highlights word AvalonEdit with bold.
一些背景信息:
AvalonEdit 是一个代码编辑器,而不是富文本编辑器。不存在“突出显示文档的一部分”这样的事情 - 文档仅存储纯文本。
突出显示是按需计算的,仅针对当前视图中的行。如果您想要自定义突出显示,则需要向突出显示计算添加一个步骤 - 这就是 mzabsky 发布的示例中的
ColorizeAvalonEdit
类所做的事情。Some background info:
AvalonEdit is a code editor, not a rich text editor. There is no such thing as "highlight a portion of the document" - the document only stores plain text.
Highlighting is computed on-demand, only for the lines currently in view. If you want custom highlighting, you need to add a step to the highlighting computation - this is what the
ColorizeAvalonEdit
class in the example posted by mzabsky is doing.您需要创建一个自定义 ColorizingTransformer 来执行此操作。上面的例子实际上是突出显示一个特定的单词。不过,您可以稍微更改它以着色或突出显示一部分。
我在我的 Console+ 项目中使用了 Avalon TextEditor(目前处于非常原始的阶段)
,你可以通过将着色器添加到 LineTransformers 集合来将其添加到编辑器。
You need to create a custom ColorizingTransformer to do that. The above example is actually highlighting a specific word. Still, you can change it a little bit to to colorize or highlight a portion.
I used Avalon TextEditor for my Console+ project (which is in a very primitive stage at the moment)
And you can add the colorizer to the editor by adding it to LineTransformers collection.
我知道这是一个很老的问题,但我想我会分享我的解决方案。我不确定这个解决方案是否已在 AvalonEdit 中实现,因为这个问题最初已得到回答,但我发现 OffsetColorizer 类实际上并没有选择该行:它只是更改了该行的背景颜色。
我的解决方案如下:
但是,这可以进一步扩展,如下所示:
我发现这个解决方案效果更好的是,它不仅仅是对线条进行着色,而是实际上选择了它:这意味着可以使用 Ctrl+C 复制它。
我希望这对未来的人们有所帮助。
I know this is a pretty old question, but I thought I would share my solution. I am not sure if this solution has been implemented into AvalonEdit, since this question was originally answered, but I find the OffsetColorizer class doesn't actually select the line: it just changes the line's background colour.
My solution is as follows:
However, this can be extended further like so:
I find that this solution works better is that rather than just colouring the line, it actually selects it: meaning it can be copied using Ctrl+C.
I Hope this helps people in the future.