如何在 RichTextBox 中获取所选内容的最后一行/段落?

How to get the last line / paragraph of a selection in a RichTextBox?

我在 RichTextBox:

的选择中有一个奇怪的行为

在选区的最后一行被完全选中的选区中,.End 属性 不指向选区的最后一行(段落)而是指向下一段。 行为截图:

所选文本

.Text属性显示正确内容

但是.End属性指向下面的paragraph

我可以遍历选择并将 .Text 属性 的内容与段落中的运行进行比较,然后......但是有没有更简单的方法来获取选择的最后一段?

我终于找到了获取选区最后一行的方法。似乎有效。

// In my case "this" is the RichTextBox itself, because I work in a derived class from RichTextBox
Paragraph lastLineInSelection = this.Selection.End.Paragraph;
if (this.Selection.End.IsAtLineStartPosition)
{
    Block previousBlock = this.Selection.End.Paragraph.PreviousBlock;
    while (previousBlock is Paragraph == false && previousBlock != null)
        previousBlock = previousBlock.PreviousBlock;

    if (previousBlock != null)
        lastLineInSelection = (previousBlock as Paragraph);
}