0

I have a scrollviewer in my WPF project. Inside the scrollviewer there are several buttons with images in the inside. Something similar to Popcorn Time (image).

There can be a lot of buttons inside the scrollviewer so it can get quite slow.

My question is, if there is an event or something similar that allows me to know what buttons are currently appearing inside the scrollviewer that the user can see, so at that moment I can load the image of the button and when it goes out of sight I can remove the image to keep the memory consumption and speed of my program a lot better. Thank you.

What I've inquired so far: There is a way to tell if a control is in sight with this method:

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);
}

If I just knew of an event that can help me using this at the moment I need, that would be great. Thanks a lot.

Sepak
  • 147
  • 1
  • 10
  • I believe you should use virtualization of the scrollviewer, something along the lines of `` . – o_weisman Dec 13 '15 at 06:16
  • If you are looking for a single, built-in event from ScrollViewer for the purpose stated, I believe there is none. In general, this one will guide you to the right direction http://stackoverflow.com/questions/1977929/wpf-listbox-with-a-listbox-ui-virtualization-and-scrolling – tgpdyk Dec 13 '15 at 13:28
  • If you look at [this SO answer](http://stackoverflow.com/a/27865101/4265041) and scroll down to the section "Only loading thumbnails when in view" there is code for a custom **ScrollViewer** that defines an attached property used to indicate whether an item is visible or not. That code may help you. – Steven Rands Dec 14 '15 at 11:48
  • Here is a solution to a similar problem: http://stackoverflow.com/questions/1517743/in-wpf-how-can-i-determine-whether-a-control-is-visible-to-the-user/42254899#42254899 – Ofer Barasofsky Feb 21 '17 at 15:54

0 Answers0