Situation
I have a 95% working application (WPF Application / .Net 4.8) that does the following:
Goal
Problem
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);
}
Finally my question
How can I determine the first visible line/block/paragraph in WPF RichTextBox?
This code gets the first visible paragraph (not necessarily the first visible line!)
TextPointer tp = richTextBox.GetPositionFromPoint(new Point(0, 0), true);
FirstParagraphLine = ((Run)tp.Paragraph.Inlines.FirstInline).Text;