我使用 AvalonEdit:TextEditor
。我能否为此控件启用快速搜索对话框(例如按下Ctrl-F)?或者也许有人有在 AvalonEdit:TextEditor
文本中搜索单词的代码吗?
我使用 AvalonEdit:TextEditor
。我能否为此控件启用快速搜索对话框(例如按下Ctrl-F)?或者也许有人有在 AvalonEdit:TextEditor
文本中搜索单词的代码吗?
关于此事的文档不多,但AvalonEdit确实有一个内置的SearchPanel类,听起来很像你想要的东西。甚至还有一个SearchInputHandler类,可以轻松地将其连接到您的编辑器上,响应键盘快捷键等。以下是一些示例代码,将标准搜索逻辑附加到编辑器:
myEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(myEditor.TextArea));
这里是一个屏幕截图,展示了搜索控件的外观(此处截取自使用AvalonEdit的ILSpy)。您可以在右上角看到搜索控件、它所支持的搜索选项以及匹配结果的自动高亮显示。
目前还不支持替换操作......但如果只需要搜索,这将是一个绝佳的解决方案。
public TextEditor() : this(new TextArea())
{
}
收件人:
public TextEditor() : this(new TextArea())
{
SearchPanel.Install(this.TextArea);
}
public TextEditorSearchable() : base(new TextArea()) {
SearchPanel.Install(TextArea);
}
}
} - scribeICSharpCode.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);
}
}
http://community.icsharpcode.net/forums/p/11536/31542.aspx#31542
你可以从这里快速添加查找/替换功能 - http://www.codeproject.com/Articles/173509/A-Universal-WPF-Find-Replace-DialogSearch.Install(...)
方法,所以我使用了下面的代码来添加搜索功能。textEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Add(new SearchInputHandler(textEditor.TextArea));
搜索框可以通过在键盘上按下Ctrl + F
来激活。
TextEditor
来扩展它。在我的一个项目中,我甚至称其为BindableTextEditor
,因为我添加的所有内容都是使其易于绑定的钩子。我不是XAML专家,所以也许有更好的方法来做到这一点(行为?),但如果您想通过XAML控制它,那就是我要走的方向。 - Stephen McDaniel