views:

208

answers:

2

In Silverlight, is there any way to get a notification event if a Control (or any FrameworkElement) has been scrolled into the viewport and is now visible?

I want to implement something like the Lazy Load Images jQuery Plugin.

+1  A: 

This Silverlight forums post from October 2009 discusses the lack of a "VisibilityChanged" event in Silverlight and comes up with the solution of using the "Loaded" event:

The Loaded event is usually a good place to start retrieving data.

With tab controls, the Loaded event for an element on a tab won't be raised until a user navigates to the tab the element is on.

I know it's not strictly analogous with your situation, but it might be worth trying to see if it works for a Control or FrameworkElement.

ChrisF
Thanks, but it's not enough for me unfortunately. :-/ ... When controls are inside panels/scrollviewers, the `Loaded` events will fire instantly, unless I use a `VirtualizingStackPanel` or the like, which I can't use because I need a more sophisticated layout, and it un-smoothes scrolling to the point of uselessness in my case.
herzmeister der welten
@herzmeister - Sorry it doesn't work for you. I can't think of anything else that would at the moment.
ChrisF
+1  A: 

Could largely solve this by now. With the help of some extension methods of the Silverlight Toolkit, we can find the inner vertical ScrollBar for any FrameworkElement by

Scrollbar myScrollBar = myContainerElement.GetVisualDescendants()
        .OfType<ScrollBar>()
        .Where(foundScrollBar => foundScrollBar.Orientation == Orientation.Vertical)
        .FirstOrDefault();

We can then attach to its events like Scroll or ValueChanged.

Then there is another helpful Toolkit extension method we can use:

Rect? rect = myElement.GetBoundsRelativeTo(myViewportElement);
if (rect.HasValue)
{
    if (rect.Value.Top <= myViewportElement.ActualHeight)
    {
        // do some stuff
    }
}
herzmeister der welten