使用GetLineStartPosition获取WPF RichTextBox中的行尾 [英] Using GetLineStartPosition to get the end of a line in WPF RichTextBox

查看:61
本文介绍了使用GetLineStartPosition获取WPF RichTextBox中的行尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点背景.我希望能够处理WPF RichTextBox中插入符号所在行的文本.请参阅我之前有关TextPointer类的问题:如何保持跟踪WPF RichTextBox中TextPointer的功能?.

A bit of background. I would like to be able to process text for the line that the caret is on in a WPF RichTextBox. Please see my earlier question about the TextPointer class: How to keep track of TextPointer in WPF RichTextBox?.

我知道如何通过使用GetLineStartPosition(以0作为参数)在当前行的开头获取TextPointer,但现在想在行尾获取TextPointer.在我之前的问题中,有人建议使用GetLineStartPosition方法可以做到这一点.

I know how to get the TextPointer at the start of the current line by using GetLineStartPosition with 0 as the argument but would now like to get the TextPointer at the end of the line. It was suggested in my previous question that this is possible using the GetLineStartPosition method.

如果有人可以解释一下关于行尾指针的GetLineStartPosition的工作原理,我将不胜感激.

I would appreciate it if someone can explain a bit about how the GetLineStartPosition works with regard to end of line pointers.

在此先感谢您的帮助.

推荐答案

GetLineStartPosition 能够为您返回行的开头,而不是行的结尾.为此,您必须将其与 GetInsertionPosition 组合.

GetLineStartPosition is able to return you the start of a line but not the end of a line. For that you will have to combine it with GetInsertionPosition.

以下是 GetLineStartPosition 的工作方式:

  • GetLineStartPosition(-1)获取上一行的开始
  • GetLineStartPosition(0)获取当前行的开头
  • GetLineStartPosition(1)获取下一行的开始
  • GetLineStartPosition(-1) gets the start of the previous line
  • GetLineStartPosition(0) gets the start of the current line
  • GetLineStartPosition(1) gets the start of the next line

您也可以使用较大的整数来调用它,以使行距更远.

You can also call it with larger integers to get lines further away.

要获取一行的末尾,只需获取下一行的开始,然后获取先前的插入位置即可.基本上是这样:

To get the end of a line just get the start of the next line, then get the prior insertion position. Basically it is this:

pointer.GetLineStartPosition(1).GetInsertionPosition(LogicalDirection.Backward);

但是,当您在文档的最后一行时,此操作不起作用: GetLineStartPosition 返回null.

However this does not work when you are on the last line of a document: GetLineStartPosition returns null.

解决此问题的简单方法是:

The easy way to fix it is to do this:

var nextStart = pointer.GetLineStartPosition(1)
var lineEnd = (nextStart !=null ? nextStart : pointer.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);

必须使用 GetInsertionPosition 而不是仅使用 GetNextContextPosition GetPointerAtOffset 移一个符号的原因是,中的每个元素FlowDocument 元素树是一个符号.因此,例如,如果当前行是表中的最后一行,则 GetLineStartPosition(1)将在该表之后的第一个段落的第一个Run中返回一个指针,而当前行的末尾是在最后一个TableCell的最后一个段落中的最后一个Run的结尾,...您就明白了.

The reason GetInsertionPosition must be used rather than just moving one symbol over using GetNextContextPosition or GetPointerAtOffset is that every element in the FlowDocument element tree is a symbol. So for example if your current line is the last line in a table, GetLineStartPosition(1) will return a pointer inside the first Run in the first Paragraph following the table, whereas the end of the current line is a the end of the last Run in the last Paragraph in the last TableCell, ... you get the idea.

最好让WPF处理在 FlowDocument 周围移动 TextPointers 的所有复杂性,这意味着使用 GetInsertionPosition 查找末尾.原始 TextPointer 指向同一行.

It is best to let WPF handle all the complexity of moving TextPointers around the FlowDocument, which means using GetInsertionPosition to find the end of the same line the original TextPointer points to.

这篇关于使用GetLineStartPosition获取WPF RichTextBox中的行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆