Page view counter
How to get a Scroll event from a ScrollViewer
Last post 06-23-2008 3:22 PM by jpsscott. 3 replies.
Sort Posts:
06-20-2008 3:24 PM
How to get a Scroll event from a ScrollViewer

Hey,

I really need a scroll event in the ScrollViewer to keep a few scrollviewer windows in synch.  It doesn't appear that SL will ever support a Scroll event so now I'm stuck trying to write my own ScrollViewer.  I figured I could do it similar to the ScrollViewer by using a ScrollContentPresenter and a couple of ScrollBars.  So far, this isn't doing the trick however.  I can't get past getting the ScrollContentPresenter to do anything.  Can't find any examples on how to do this either.

So could someone:

a) recommend a way to get a scroll event from the ScrollViewer?

b) Show me how to use the ScrollContentPresenter control to display something?

I use xaml like this for ScrollContentPresenter <ScrollContentPresenter Content="Some Content"/>  I've also tried inserting content like <ScrollContentPresenter.Content> underneath the ScrollContentPresenter tag.  Unfortunately I'm getting nowhere.

Please advise...

James

jpsscott

Loading...
Joined on 03-10-2008
Posts 76
06-20-2008 4:38 PM
Re: How to get a Scroll event from a ScrollViewer

I created a custom ScrollViewer by wrapping the existing one with a custom control.  I made a TemplatePart of type ScrollViewer, which my default style defined.  Then I made my own corresponding "HorizontalOffset", "ScrollToVerticalOffset()", etc members that passed on the call to the actual scroll viewer.  If you did this then you could make a callback on your custom dependency properties that fires an event.

Of course, a scroll event would be much cleaner, so hopefully MSFT will implement it in a later version.

[Edit: On second thought, this probably won't work for you if you are still letting the user scroll using the scroll bars.  In my case I implemented a different method of scrolling (dragging on the content to "pan") so the scroll viewer's properties never changed except through my custom control.]

dcstraw

Loading...
Joined on 05-16-2008
Posts 83
06-20-2008 5:02 PM
Re: How to get a Scroll event from a ScrollViewer

OK I thought of a different way but it's kind of ugly.  You can handle the LayoutUpdated event of the scroll viewer like this:

void ScrollViewer_LayoutUpdated(object sender, EventArgs e)
{
    if (!_SkipLayoutEvent)
    {
        _SkipLayoutEvent = true;

        OnScroll();
    }
    else
    {
        _SkipLayoutEvent = false;
    }
}

The _SkipLayoutEvent was necessary because any time I updated any other part of the UI it kicked off the ScrollViewer's LayoutUpdated event again, even if the updated UI wasn't inside the scroll viewer.  I'm not sure why this happens, but if you don't account for it you get layout error due to an infinite loop.  The biggest downside to this implementation is that the event will get kicked off for a lot of things besides scrolling, but it does at least get fired off every time the user scrolls.

dcstraw

Loading...
Joined on 05-16-2008
Posts 83
06-23-2008 3:22 PM
Marked as Answer
Re: How to get a Scroll event from a ScrollViewer

Alright,

Very happy to say that I have got something that works well for me and I hope others will be able to use it too!  Use a class called VisualTreeHelper.GetChild to get a reference to the first child in the ScrollViewer, cast it to a frameworkelement or whatever to call FindName on it, then use FindName to do a search for "VerticalScrollBar" or "HorizontalScrollBar".  From there, listen to the scroll event.

Make sure you do it from the LayoutUpdated event on the scrollviewer or after so that the VisualTreeHelper class picks up the children.  I found that before the layout_udpated event it did not.

James

jpsscott

Loading...
Joined on 03-10-2008
Posts 76
Microsoft Communities