Question

I have a VariableSizeGridView (aka GridView with a VariableSizedWrapGrid) and since the framework doesn't support ISupportIncrementalLoading on VariableSizedWrapGrid I implemented my own by "listening" to the GridView parent ScrollViewer (this specific case a Hub control)

Everything works fine, when I reach on the end of the scrollviewer my code calls a routine to get more data.

The problem occurs when the new data gets on the screen, the scrollviewer automatically scrolls to the end and the load process starts again, ending up in an infinite loop if I don't move the scrollbar elsewhere.

Is there any way to prevent that the scrollviewer automatically goes to the end after I added some items?

Thanks

Was it helpful?

Solution

You can create a Custom-control that overrides from VariableSizeGridView and listen, as you do, to the scrollviewer. Since you have access to the latest element of the GridView, after starting loading more elements, you can set the scrollviewer to the that latest position position. Get a visual reference to that element and then call this code:

FrameworkElement focusedElement = FocusManager.GetFocusedElement() as FrameworkElement;
        GeneralTransform focusedVisualTransform = parent.TransformToVisual(_scrollViewer);

ApplyHorizontalScrolling(focusedElement, focusedVisualTransform);

  private void ApplyHorizontalScrolling(FrameworkElement focusedElement, GeneralTransform focusedVisualTransform)
    {
        Rect rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
        double horizontalOffset = _scrollViewer.HorizontalOffset + (rectangle.Left);
        _scrollViewer.ChangeView(horizontalOffset, 0, _scrollViewer.ZoomFactor);
    }

Using and tunning this code, will help you preventing the scrollbar to go to the latest position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top