GetLineStartPosition can return you the beginning of a line, but not the end of a line. To do this, you will have to combine it with GetInsertionPosition .
Here's GetLineStartPosition works:
GetLineStartPosition(-1) gets the beginning of the previous lineGetLineStartPosition(0) gets the beginning of the current lineGetLineStartPosition(1) gets the start of the next line
You can also call it with large integers to get the lines further.
To get the end of a line, simply start the beginning of the next line, then get the previous insertion position. Mainly:
pointer.GetLineStartPosition(1).GetInsertionPosition(LogicalDirection.Backward);
However, this does not work when you are on the last line of the document: GetLineStartPosition returns null.
Easy way to fix this:
var nextStart = pointer.GetLineStartPosition(1) var lineEnd = (nextStart !=null ? nextStart : pointer.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);
You must use the GetInsertionPosition argument, and not just move a single character using GetNextContextPosition or GetPointerAtOffset in that each element in the FlowDocument element tree is a symbol. For example, if your current row is the last row in the table, GetLineStartPosition(1) will return the pointer inside the first run in the first paragraph following the table, while the end of the current row is the end of the last run in the last paragraph in the last TableCell, .. . you get the idea.
It is best to allow WPF the complexity of moving TextPointers around a FlowDocument , which means using GetInsertionPosition to find the end of the same line that the original TextPointer .
Ray burns
source share