gpt4 book ai didi

c# - AvalonEdit 作为文本查看器 - 无插入符号

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:46 26 4
gpt4 key购买 nike

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

我可以:

textEditor.IsReadOnly = true;

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

有什么办法可以让它成为viewer吗?或者至少,隐藏插入符号?

最佳答案

AvalonEdit 由三个组件组成:

  • TextEditor,它在 ScrollViewer 中包装了一个 TextArea 并添加了一个类似于 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);

关于c# - AvalonEdit 作为文本查看器 - 无插入符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com