AvalonEdit作为文本查看器 - 没有插入符号

时间:2017-04-27 09:39:07

标签: c# wpf avalonedit

我希望AvalonEdit成为文本查看器。

我可以:

textEditor.IsReadOnly = true;

并且控件不允许进行任何更改,但它仍然像编辑器一样 - 显示插入符号,并使用导航键(箭头,页面向上/向下)移动插入符号而不是滚动视图。

有没有办法让它成为观众?或者至少,隐藏插入符号?

2 个答案:

答案 0 :(得分:3)

AvalonEdit由三个部分组成:

  • TextEditor,它将TextArea包裹在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是不够的。 在这种情况下,您需要继续使用TextEditorTextArea,并停用不需要的功能。

你不能真正禁用插入符,因为选择逻辑依赖于插入符号,但你可以隐藏它:

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

答案 1 :(得分:1)

尝试将IsHitTestVisible属性设置为false

textEditor.IsHitTestVisible = false;
  

它确实隐藏了插入符号,但很多东西不再起作用了,即。用鼠标滚轮滚动

如果您只想隐藏插入符号,可以将其CaretBrush属性设置为Transparent

textEditor.TextArea.Caret.CaretBrush = Brushes.Transparent;
相关问题