[英]AvalonEdit as a text viewer - no caret
我希望AvalonEdit成為文本查看器。
我會做:
textEditor.IsReadOnly = true;
並且該控件不允許進行任何更改,但它的行為仍像編輯器一樣-顯示插入符號,並使用導航鍵(箭頭,上/下翻頁)移動插入符號,而不滾動視圖。
有什么辦法可以使其成為觀眾? 還是至少隱藏了插入符?
AvalonEdit由三個組件組成:
TextEditor
,它將TextArea
包裝在ScrollViewer
並添加ScrollViewer
TextBox
的高級API TextArea
,它接受一個TextView
並添加脫字號,選擇和輸入處理 TextView
,這是實際的代碼顯示 因此,要禁用所有編輯功能,可以直接使用TextView
類。 要啟用滾動,您需要自己將其包裝在ScrollViewer
(重要:啟用CanContentScroll
以避免呈現文檔的不可見部分)
<ScrollViewer
Focusable="False"
CanContentScroll="True"
VerticalContentAlignment="Top"
HorizontalContentAlignment="Left">
<avalonedit:TextView Name="textView" />
</ScrollViewer>
通過直接使用TextView
組件,您需要完成一些通常由TextEditor
自己完成的工作:
textView.Document = new TextDocument(); // create document instance
textView.LineTransformers.Insert(0,
new HighlightingColorizer(HighlightingManager.Instance.GetDefinition("C#")));
如果您想保留一些編輯功能(例如,選擇文本並將其復制到剪貼板),則TextView
是不夠的。 在這種情況下,您將需要繼續使用TextEditor
或TextArea
,並禁用不需要的功能。
您不能真正禁用插入符號,因為選擇邏輯取決於是否有插入符號,但是您可以將其隱藏:
textEditor.TextArea.Caret.CaretBrush = Brushes.Transparent;
將文檔設為只讀將禁用文本輸入和各種編輯命令:
textEditor.IsReadOnly = true;
您可能還想從文本區域的輸入處理程序中刪除命令:
// remove the keyboard caret navigation and selection logic,
// but keep the mouse selection logic and editing commands
textEditor.TextArea.DefaultInputHandler.NestedInputHandlers.Remove(
textEditor.TextArea.DefaultInputHandler.CaretNavigation);
嘗試將IsHitTestVisible
屬性設置為false
:
textEditor.IsHitTestVisible = false;
它確實隱藏了插入符,但是很多事情不再起作用了。 用鼠標滾輪滾動
如果你只想隱藏,你可以設置其插入符號CaretBrush
財產Transparent
:
textEditor.TextArea.Caret.CaretBrush = Brushes.Transparent;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.