1

I have a problem with setting Scroll Viewer vertical scroll position (C#, Windows Store App).

I have a FlipView, which contains 2 to 5 items. Item is my own user control - Grid in Scroll Viewer. I need to change scroll position to all FlipView items, when current selected FlipView item scroll position has been changed.

In my user control I use event:

private void MyScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
    _myPage.ChangeFlipViewItemsScrollPosition(MyScrollViewer.VerticalOffset);
}

In my page I use method:

public void ChangeFlipViewItemsScrollPosition(double verticalOffset)
{
    for (int i = 0; i < MyFlipView.Items.Count; i++)
    {
        MyUserControl item = MyFlipView.Items[i] as MyUserControl;
        if (item != null && i != MyFlipView.SelectedIndex)
        {
            item.MyScrollViewer.ChangeView(null, verticalOffset, null);
        }
    }
}

The problem is that:

For example, I have 5 items in FlipView.

When FlipView selected index is:

0 then it changes scroll position to items with index: 1 and 2;

1 then it changes scroll position to items with index: 0 and 2;

2 then it changes scroll position to items with index: 0, 1 and 3;

3 then it changes scroll position to items with index: 2 and 4;

4 then it changes scroll position to item with index: 3;

I have no idea, why it is happening. Can anyone help with this?

  • What is your ItemsPanel in FlipView? Can this be somehow related to virtualization? Put a breakpoint on the first line inside the `if` and see what type the other items are. – molnarm May 08 '15 at 19:55
  • @Márton Molnár Tnx you for a comment...I just realized that i need to delete the virtualization from Stack panel and then all works fine. – Kristaps Ošiņs May 11 '15 at 07:25

1 Answers1

0

Looks like your FlipView has an item container which is using virtualization to enhance performance. That means that the controls for items that aren't currently visible may be discarded or reused.

I didn't find any Windows Store app-specific documentation, but the behavior is probably very similar to that of WPF, explained here. See some more code here.

You can turn it off by either setting VirtualizingStackPanel.IsVirtualizing to false on your FlipView, or replacing the container with a plain StackPanel (or any other non-virtualizing container).

Community
  • 1
  • 1
molnarm
  • 9,856
  • 2
  • 42
  • 60