WPF RichTextBox:查找当前在 RichTextBox 中可见的第一个 block/paragraph

WPF RichTextBox: Find the first block/paragraph that is currently visible in the RichTextBox

情况

我有一个 95% 的工作应用程序(WPF 应用程序/.Net 4.8)执行以下操作:

目标

问题

string text = null;
foreach (var block in RichTextBox.Document.Blocks)
{
      //This does not work. Block is not a FrameworkElement...shit...
      var blockIsVisible = IsUserVisible(block, RichTextBox);
      if (blockIsVisible)
      {
          var text = new TextRange(block.ContentStart, block.ContentEnd).Text;
          break;
      }
}

if (!string.IsNullOrWhiteSpace(text))
{
    //Scroll to given text in HTML with JQuery...
}

private bool IsUserVisible(FrameworkElement element, FrameworkElement container)
{
    if (!element.IsVisible) return false;
   
    Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
    Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
    return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}

最后是我的问题

如何确定 WPF RichTextBox 中第一个 visible 行/block/paragraph?

此代码获取第一个可见段落(不一定是第一个可见行!)

    TextPointer tp = richTextBox.GetPositionFromPoint(new Point(0, 0), true);
    FirstParagraphLine = ((Run)tp.Paragraph.Inlines.FirstInline).Text;