【问题标题】:Does AvalonEdit :TextEditor have quick search/replace functionality?AvalonEdit :TextEditor 是否具有快速搜索/替换功能?
【发布时间】:2012-10-31 22:50:16
【问题描述】:

我使用AvalonEdit:TextEditor。我可以为此控件启用快速搜索对话框(例如在 Ctrl-F 上)吗?或者也许有人将搜索词的代码放入AvalonEdit:TextEditor 文本中?

【问题讨论】:

    标签: c# search avalonedit


    【解决方案1】:

    关于它的文档不多,但 AvalonEdit 确实有一个内置的 SearchPanel 类,听起来完全符合您的要求。甚至还有一个 SearchInputHandler 类,可以轻松地将其连接到您的编辑器、响应键盘快捷键等。下面是一些将标准搜索逻辑附加到编辑器的示例代码:

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

    这是它的外观截图(取自使用 AvalonEdit 的ILSpy)。您可以看到右上角的搜索控件、它支持的搜索选项以及匹配结果的自动突出显示。

    不支持替换...但是如果您只需要搜索,这可能是一个很好的解决方案。

    【讨论】:

    • 对于 XAML?不可能吗?
    • 当谈到 AvalonEdit 时,我希望有很多东西可以在 XAML 中轻松使用。我通常最终从TextEditor 继承来扩展它。在我的一个项目中,我想我什至称它为BindableTextEditor,因为我添加的只是钩子,以便于绑定。我不是 XAML 专家,所以也许有更好的方法来做到这一点(行为?),但如果你想通过 XAML 控制它,这就是我要走的方向。
    【解决方案2】:

    对于 Avalon Edit 5.0.1.0 及更高版本,只需执行以下操作:

    SearchPanel.Install(XTBAvalonEditor);
    

    其中 XTBAvalonEditor 是 WPF AvalonEdit 控件名称。

    确保添加这个 using 语句:

    using ICSharpCode.AvalonEdit.Search;
    

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

    【讨论】:

    • 除此之外还有什么可做的吗?以这种方式“安装”后,当我点击 CTRL-F 时它会显示出来,但我无法输入。
    【解决方案3】:

    在 ICSharpCode.AvalonEdit 项目的 TextEditor 构造函数中,添加 SearchPanel.Install(this.TextArea);瞧,使用 ctrl+f 打开搜索窗口。

    (使用 Stephen McDaniel 帖子中的行(用此替换 myEditor)也可以,但正在删除对 SearchInputHandler 的支持)

    (与 AvalonDock 中的 AvalonEdit 和 MVVM 配合使用效果很好)

    发件人:

    public TextEditor() : this(new TextArea())
    {
    }
    

    收件人:

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

    【讨论】:

      【解决方案4】:

      我最后一次检查它是“否”。您必须实现自己的搜索/替换功能。

      http://community.icsharpcode.net/forums/p/11536/31542.aspx#31542

      您可以从这里快速添加查找/替换 - http://www.codeproject.com/Articles/173509/A-Universal-WPF-Find-Replace-Dialog

      【讨论】:

        【解决方案5】:

        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);
                }
            }
        

        【讨论】:

          猜你喜欢
          • 2018-04-14
          • 2015-09-08
          • 1970-01-01
          • 2011-03-19
          • 2014-06-15
          • 2015-10-04
          • 1970-01-01
          • 1970-01-01
          • 2021-11-21
          相关资源
          最近更新 更多