I have a FlowDocument with much content inside. I need to get the controls that are currently in the visible area.
With the following code I was able to get the current scrolling Position.
DependencyObject obj = FlowDocumentScrollViewerCtrl;
do
{
if (VisualTreeHelper.GetChildrenCount(obj) > 0)
{
obj = VisualTreeHelper.GetChild(obj as Visual, 0);
}
}
while (!(obj is ScrollViewer));
ScrollViewer sv = obj as ScrollViewer;
How can I get the controls within the visible area?
One way is to recursively descend the Visual tree using
VisualTreeHelper.GetChildrenCountandVisualTreeHelper.GetChild()and check each Visual using this procedure:new Rect(0, 0, visual.ActualWidth, visual.ActualHeight). This will give you the bounding box in the Visual's coordinate system.visual.TransformToAncestor(viewer)to transform the bounding box into the viewer's coordinate system.This will tell you all the visuals in the visible area. If you want to map to
FrameworkContentElements such as<Paragraph>, just check out the Content property of theContentPresenters you run across in your tree walk.