2
我想突出顯示AvalonEdit中選定(突出顯示)文本的所有實例。 VS2010可以做到這一點,它是一個方便的功能。我知道我需要按照以下代碼實現DocumentColorizingTransformer,但不知道如何從文檔中獲取所選文本。選擇信息在「CurrentContext」中不可用。選擇AvalonEdit中突出顯示的單詞的所有實例
下面的代碼找到「AvalonEdit」的所有實例。我如何找到所選(突出顯示)文本的所有實例。
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
} } }
「TextEditor上提供了當前的文本選擇,因此您可以使用ColorizeAvalonEdit類中的文本選項。」這是這個問題的重要組成部分。你如何從CurrentContext訪問textEditor。它似乎不在那裏? – paligap
正確,它在CurrentContext上不可用,因此您必須從其他位置獲取它。查看SharpDevelop源代碼,似乎是如果自定義着色變壓器需要文本編輯器,那麼它將在創建時傳遞到變壓器中。 –
如果您不喜歡在構造函數中傳遞TextEditor,則可以將其添加到TextView,因爲它實現了IServiceProvider接口。 TextView在CurrentContext中可用。 –