Detect when ScrollViewer has stopped scrolling

你说的曾经没有我的故事 提交于 2019-12-08 05:52:37

问题


I have a SurfaceListBox that inside has a ScrollViewer. I have access to that ScrollViewer and I want to detect when the scrolling ends. Since it is a touch app and I can drag it, right now I'm detecting the PreviewTouchUp to do what I want, however, if I do a fast swipe, like you would do on a smartphone, because the content of the ScrollViewer is still dragging/scrolling, the PreviewTouchUp does not reflect the end of the scrolling, but is instead triggered before.

I have tried Manipulation events and several others on both the ScrollViewer and the SurfaceListBox. I have also tried ScrollViewer.PanningDeceleration and honestly, I don't see a difference. Basically, no luck so far.

So, how can I detect the end of the scrolling, even after the PreviewTouchUp in a ScrollViewer?


回答1:


Can't you just handle the ScrollViewer.ScrollChanged event? From the linked page, it says that it...

Occurs when changes are detected to the scroll position, extent, or viewport size.

So you could just start a DispatcherTimer to wait for a short instant each time the event has been raised, eg. when it is scrolling. When the scrolling stops and the event stops being raised, then the Tick handler will be called and you can do whatever you like in it. Try something like this:

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 250);
timer.Tick += Timer_Tick;

...

private void ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (timer.IsEnabled) timer.Stop();
    timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{
    timer.Stop();
    // Do what you want now that scrolling has stopped
}



回答2:


Seems a bit messy but Sheridan's solution was the best way I could find to determine when a scrollviewer has stopped moving. I just tweaked the timer interval and, more importantly, put the "// Do what you want now that scrolling has stopped" between hittestvisible = false and then hittestvisible = true on the scrollviewer control.



来源:https://stackoverflow.com/questions/21586381/detect-when-scrollviewer-has-stopped-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!