none
scroll viewer events

    General discussion

  • Is there a way to detect if the ScrollViewer was flicked and is still scrolling from the momentum? Or would I have to subtract the positions to see if it has moved? 

    Wednesday, April 18, 2012 7:24 PM

All replies

  • Hi,

    Silverlight ScrollViewer doesnt have events to listen the scroll change, but the scroll bar will have the events to listen this. So you can get the ScrollBar in Scroll view by iterate through the VisulTree. By hooking the events with that scroll bar you can listen to the chagne.

    Name of Horizontal scroll bar is "HorizontalScrollBar", name of vertical is  "VerticalScrollBar"

               /// Finding Scroll Bar
                ScrollBar hscrollBar = (ScrollBar)this.Scroller.FinElementWithName("HorizontalScrollBar");
    
                /// Hooking events
                hscrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(hscrollBar_ValueChanged);
                hscrollBar.Scroll += new ScrollEventHandler(hscrollBar_Scroll);

    I have used Extension class to iterate through the visual tree.

    public static class DependencyObjectExtensions
        {
            public static FrameworkElement FinElementWithName(this FrameworkElement element, string name)
            {
                FrameworkElement obj = element as FrameworkElement;
                if (obj != null && obj.Name.Equals(name))
                {
                    return obj;
                }
    
                if (element != null)
                {
                    int numChildren = VisualTreeHelper.GetChildrenCount(element);
                    for (int i = 0; i < numChildren; i++)
                    {
                        FrameworkElement child = FinElementWithName(VisualTreeHelper.GetChild(element, i) as FrameworkElement, name);
                        if (child != null)
                        {
                            return child;
                        }
                    }
    
                    // Popups continue in another window, jump to that tree
                    Popup popup = element as Popup;
                    if (popup != null)
                    {
                        return FinElementWithName(popup.Child as FrameworkElement, name);
                    }
                }
                return null;
            }
        }

     

    Hope this will helps you.

    -Sri.

    Thursday, April 19, 2012 2:07 AM
  • My program crashes with a NullReferenceException on 

    hscrollBar.Scroll += new ScrollEventHandler(hscrollBar_Scroll);

    Which means the function isnt returning the scrollbar? Is my scrollViewer supposed to be the this.Scroller?

    
    
    Thursday, April 19, 2012 1:45 PM
  • Hi,

    I have named my scroll viewer as "scroller", hence my code will have that name. Based on your code you have to change the name. And also you can use this method only when the scroll view's template has applied. To ensrue the template applied you can use of LayoutUpdated event of ScrollViewer but use that with a flag, because it will be invoke frequently.

    -Sri.

    Friday, April 20, 2012 12:43 AM