【发布时间】:2012-10-31 22:50:16
【问题描述】:
我使用AvalonEdit:TextEditor。我可以为此控件启用快速搜索对话框(例如在 Ctrl-F 上)吗?或者也许有人将搜索词的代码放入AvalonEdit:TextEditor 文本中?
【问题讨论】:
标签: c# search avalonedit
我使用AvalonEdit:TextEditor。我可以为此控件启用快速搜索对话框(例如在 Ctrl-F 上)吗?或者也许有人将搜索词的代码放入AvalonEdit:TextEditor 文本中?
【问题讨论】:
标签: c# search avalonedit
关于它的文档不多,但 AvalonEdit 确实有一个内置的 SearchPanel 类,听起来完全符合您的要求。甚至还有一个 SearchInputHandler 类,可以轻松地将其连接到您的编辑器、响应键盘快捷键等。下面是一些将标准搜索逻辑附加到编辑器的示例代码:
myEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(myEditor.TextArea));
这是它的外观截图(取自使用 AvalonEdit 的ILSpy)。您可以看到右上角的搜索控件、它支持的搜索选项以及匹配结果的自动突出显示。
不支持替换...但是如果您只需要搜索,这可能是一个很好的解决方案。
【讨论】:
TextEditor 继承来扩展它。在我的一个项目中,我想我什至称它为BindableTextEditor,因为我添加的只是钩子,以便于绑定。我不是 XAML 专家,所以也许有更好的方法来做到这一点(行为?),但如果你想通过 XAML 控制它,这就是我要走的方向。
在 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);
}
【讨论】:
我最后一次检查它是“否”。您必须实现自己的搜索/替换功能。
http://community.icsharpcode.net/forums/p/11536/31542.aspx#31542
您可以从这里快速添加查找/替换 - http://www.codeproject.com/Articles/173509/A-Universal-WPF-Find-Replace-Dialog
【讨论】:
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);
}
}
【讨论】: