Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

c# - Using GetLineStartPosition to get the end of a line in WPF RichTextBox

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?.

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.

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

Thanks in advance for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Here's how GetLineStartPosition works:

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

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...