AvalonEdit:TextEditor有快速搜索/替换功能吗?

17

我使用 AvalonEdit:TextEditor。我能否为此控件启用快速搜索对话框(例如按下Ctrl-F)?或者也许有人有在 AvalonEdit:TextEditor 文本中搜索单词的代码吗?

6个回答

28

关于此事的文档不多,但AvalonEdit确实有一个内置的SearchPanel类,听起来很像你想要的东西。甚至还有一个SearchInputHandler类,可以轻松地将其连接到您的编辑器上,响应键盘快捷键等。以下是一些示例代码,将标准搜索逻辑附加到编辑器:

myEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(myEditor.TextArea));

这里是一个屏幕截图,展示了搜索控件的外观(此处截取自使用AvalonEdit的ILSpy)。您可以在右上角看到搜索控件、它所支持的搜索选项以及匹配结果的自动高亮显示。

在 ILSpy 中使用 SearchPanel 进行搜索

目前还不支持替换操作......但如果只需要搜索,这将是一个绝佳的解决方案。


而对于XAML呢?这不可能吗? - Alexsandro
当涉及到AvalonEdit时,我希望XAML中有很多东西是易于使用的。通常我会继承TextEditor来扩展它。在我的一个项目中,我甚至称其为BindableTextEditor,因为我添加的所有内容都是使其易于绑定的钩子。我不是XAML专家,所以也许有更好的方法来做到这一点(行为?),但如果您想通过XAML控制它,那就是我要走的方向。 - Stephen McDaniel

25

对于 Avalon Edit 版本 5.0.1.0 及以上,只需这样做:

SearchPanel.Install(XTBAvalonEditor);

其中,XTBAvalonEditor是WPF AvalonEdit控件的名称。

请确保添加以下using语句:

using ICSharpCode.AvalonEdit.Search;

然后当编辑器获得焦点时,按下CTL-F:您将看到查找控件弹出在右上角。

输入图像描述


除此之外还有其他事情要做吗?我以这种方式“安装”后,在我点击CTRL-F时会显示,但我无法在其中输入。 - Gimly
这对我来说完美地运作,即使是使用AvalonEdit的6.3.0.90版本,非常感谢。:-) - holbizmetrics

13
在ICSharpCode.AvalonEdit项目的TextEditor构造函数中,添加SearchPanel.Install(this.TextArea); 然后使用Ctrl + F即可打开搜索窗口。(在Stephen McDaniel的帖子中使用其行(用this替换myEditor)也可以,但是对于SearchInputHandler的支持将被移除) (在MVVM下的AvalonDock中使用AvalonEdit效果很好) 来自:
public TextEditor() : this(new TextArea())
{
}

收件人:

public TextEditor() : this(new TextArea())
{
  SearchPanel.Install(this.TextArea);
}

最好自己创建派生类:using ICSharpCode.AvalonEdit; using ICSharpCode.AvalonEdit.Editing; using ICSharpCode.AvalonEdit.Search;namespace SVitLAB.WinTail.Views.Controls { public class TextEditorSearchable : TextEditor { public TextEditorSearchable() : base(new TextArea()) { SearchPanel.Install(TextArea); } }} - scribe

4

ICSharpCode.AvalonEdit 4.3.1.9429

搜索和高亮显示项目。

private int lastUsedIndex = 0;
        public void Find(string searchQuery)
        {
            if (string.IsNullOrEmpty(searchQuery))
            {
                lastUsedIndex = 0;
                return;
            }

            string editorText = this.textEditor.Text;

            if (string.IsNullOrEmpty(editorText))
            {
                lastUsedIndex = 0;
                return;
            }

            if (lastUsedIndex >= searchQuery.Count())
            {
                lastUsedIndex = 0; 
            }

            int nIndex = editorText.IndexOf(searchQuery, lastUsedIndex);
            if (nIndex != -1)
            {
                var area = this.textEditor.TextArea;
                this.textEditor.Select(nIndex, searchQuery.Length);
                lastUsedIndex=nIndex+searchQuery.Length;
            }
            else
            {
                lastUsedIndex=0;
            }
        }

替换操作:

public void Replace(string s, string replacement, bool selectedonly)
        {
            int nIndex = -1;
            if(selectedonly)
            {
                nIndex = textEditor.Text.IndexOf(s, this.textEditor.SelectionStart, this.textEditor.SelectionLength);           
            }
            else
            {
                nIndex = textEditor.Text.IndexOf(s);
            }

            if (nIndex != -1)
            {
                this.textEditor.Document.Replace(nIndex, s.Length, replacement);


    this.textEditor.Select(nIndex, replacement.Length);
        }
        else
        {
            lastSearchIndex = 0;
            MessageBox.Show(Locale.ReplaceEndReached);
        }
    }

4

0
在我的情况下,我找不到Search.Install(...)方法,所以我使用了下面的代码来添加搜索功能。
textEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(textEditor.TextArea));

搜索框可以通过在键盘上按下Ctrl + F来激活。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接