2012-11-12 88 views

回答

20

沒有關於它的多文檔,但AvalonEdit確實有在SearchPanel類,聽起來完全像你想要的建造。甚至有一個SearchInputHandler類,它使平凡得到它掛接到你的編輯器,響應快捷鍵,等在這裏的是,連接標準的搜索邏輯編輯一些示例代碼:

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

這裏它是什麼樣子的截圖(這取自使用AvalonEdit的ILSpy)。您可以在右上角看到搜索控件,它支持的搜索選項以及自動突出顯示的匹配結果。

Searching in ILSpy with SearchPanel

沒有對更換任何支持......但如果你只需要搜索,這可能是一個很好的解決方案。

+0

而對於XAML?這不可能? –

+0

當談到AvalonEdit時,我希望在XAML中使用很多很容易的東西。我通常最終從'TextEditor'繼承來擴展它。在我的一個項目中,我認爲我甚至稱它爲'BindableTextEditor',因爲我添加的所有內容都是鉤子,以便於綁定。我不是XAML專家,所以也許有更好的方法來做到這一點(行爲?),但如果你想通過XAML來控制這一點,那麼這就是我要做的方向。 –

+0

輝煌!謝謝! – peter70

2

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

在ICSharpCode.AvalonEdit項目的TextEditor構造函數中,添加SearchPanel.Install(this.TextArea);瞧,使用Ctrl + F打開搜索窗口。

(使用斯蒂芬麥克丹尼爾的交行(更換此MyEditor的)也可以,但是對於SearchInputHandler的支持已經被刪除)

從(裏面AvalonDock AvalonEdit與MVVM效果很好):

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

要:

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

Avalon的編輯版本5.0.1.0,只是這樣做:

SearchPanel.Install(XTBAvalonEditor); 

哪裏XTBAvalonEditor是WPF AvalonEdit控件名稱。

確保添加此using語句:

using ICSharpCode.AvalonEdit.Search; 

然後當編輯器有重點,按CTL-F:你會看到查找控制在右上角彈出。

enter image description here

+0

除此之外還有什麼可以做的嗎?在以這種方式「安裝」之後,當我點擊CTRL-F時顯示,但我無法輸入。 – Gimly