令人难以置信的令人敬畏的AvalonEdit WPF TextEditor控件似乎缺少一个重要的功能,或者至少我无法弄明白。 给定偏移量和长度,使用HighlightColor 突出显示TextDocument中的该部分。简单,对吧?
显然不是。我有RTFM,关于“语法突出显示”的文档让我更加困惑。 Someone else asked the same question in the SharpDevelop forums,我恐怕无法理解格伦沃尔德先生的回答。
这是我的尝试,使用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 });
感谢您的帮助!
答案 0 :(得分:7)
一些背景信息: AvalonEdit是代码编辑器,而不是富文本编辑器。没有“突出显示文档的一部分” - 文档只存储纯文本。
按需突出显示计算,仅适用于当前视图中的行。如果您想要自定义突出显示,则需要在突出显示计算中添加一个步骤 - 这就是mzabsky发布的示例中的ColorizeAvalonEdit
类正在执行的操作。
答案 1 :(得分:5)
你是否在this article中看到了这一点 - 它似乎正是你要求的:
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
} } }
用粗体突出显示单词AvalonEdit。
答案 2 :(得分:4)
您需要创建自定义ColorizingTransformer才能执行此操作。上面的例子实际上突出显示了一个特定的单词。不过,您可以稍微更改一下以着色或突出显示部分。
我将Avalon TextEditor用于我的Console+项目(目前处于一个非常原始的阶段)
public class OffsetColorizer : DocumentColorizingTransformer
{
public int StartOffset { get; set; }
public int EndOffset { get; set; }
protected override void ColorizeLine(DocumentLine line)
{
if (line.Length == 0)
return;
if (line.Offset < StartOffset || line.Offset > EndOffset)
return;
int start = line.Offset > StartOffset ? line.Offset : StartOffset;
int end = EndOffset > line.EndOffset ? line.EndOffset : EndOffset;
ChangeLinePart(start, end, element => element.TextRunProperties.SetForegroundBrush(Brushes.Red));
}
}
您可以通过将颜色添加到LineTransformers集合中来将颜色添加到编辑器中。
tbxConsole.TextArea.TextView.LineTransformers.Add(_offsetColorizer);
答案 3 :(得分:1)
我知道这是一个非常古老的问题,但我想我会分享我的解决方案。我不确定这个解决方案是否已经在AvalonEdit中实现,因为这个问题最初得到了回答,但是我发现OffsetColorizer类实际上没有选择该行:它只是改变了行的背景颜色。
我的解决方案如下:
textEditor.SelectionStart = offset;
textEditor.SelectionLength = length;
但是,这可以进一步扩展:
public void SelectText(int offset, int length)
{
//Get the line number based off the offset.
var line = textEditor.Document.GetLineByOffset(offset);
var lineNumber = line.LineNumber;
//Select the text.
textEditor.SelectionStart = offset;
textEditor.SelectionLength = length;
//Scroll the textEditor to the selected line.
var visualTop = textEditor.TextArea.TextView.GetVisualTopByDocumentLine(lineNumber);
textEditor.ScrollToVerticalOffset(visualTop);
}
我发现这个解决方案效果更好的是,它不仅仅是对线条着色,而是实际选择它:意味着可以使用Ctrl + C复制它。
我希望这有助于将来的人们。