locked
ListView - Scroll to specific item

    Question

  • I have a ListView:

    <ListView ItemsSource="{Binding}" x:Name="LvLevels" 
                      Margin="10 60 0 0" 
                      SelectionChanged="LvLevels_SelectionChanged">
    
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                </Style>
            </ListView.ItemContainerStyle>
    
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
    
                        <Border BorderBrush="White" BorderThickness="6">
                            <TextBlock Text="{Binding}" 
                                       FontSize="56" FontWeight="ExtraBold"
                                       Margin="10"
                                       HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                       TextAlignment="Center">
    
    
                            </TextBlock>
                        </Border>
    
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    I need to select specific Item and scroll ListView to it. I can select Item:

    LvLevels.SelectedItem = CurrentLevel;

    But I cannot to scroll ListView to this item. Here is my code:

    LvLevels.ScrollIntoView(LvLevels.SelectedItem);

    Here is a code which set DataContext and set Selected Item:

            List<int> data = new List<int>();
            for (int i = 0; i <= 20; i++)
                data.Add(i);
    
            LvLevels.DataContext = data;
    
            LvLevels.SelectedItem = LvLevels.Items[14];
            LvLevels.ScrollIntoView(LvLevels.SelectedItem);


    Friday, January 09, 2015 2:55 PM

Answers

  • With a button click event, scrollIntoView works fine. Can you also confirm it?

    Use following code should fix for you, I may not call it as a solution but it could be a workaround, because the UI need time to be ready.

                List<int> data = new List<int>();
                for (int i = 0; i <= 20; i++)
                    data.Add(i);
    
                LvLevels.DataContext = data;
    
                TimeSpan period = TimeSpan.FromMilliseconds(10);
                Windows.System.Threading.ThreadPoolTimer.CreateTimer(async (source) =>
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        LvLevels.SelectedItem = LvLevels.Items[14];
                        LvLevels.ScrollIntoView(LvLevels.SelectedItem);
                    });
                }
                , period);

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 8:21 AM
    Moderator

All replies

  • what does LvLevels.ScrollIntoView(LvLevels.SelectedItem); do?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, January 09, 2015 3:53 PM
  • I hoped it will scroll my ListView to the selected item, but it doesn't work.  Nothing happen on this line.
    Friday, January 09, 2015 4:03 PM
  • With a button click event, scrollIntoView works fine. Can you also confirm it?

    Use following code should fix for you, I may not call it as a solution but it could be a workaround, because the UI need time to be ready.

                List<int> data = new List<int>();
                for (int i = 0; i <= 20; i++)
                    data.Add(i);
    
                LvLevels.DataContext = data;
    
                TimeSpan period = TimeSpan.FromMilliseconds(10);
                Windows.System.Threading.ThreadPoolTimer.CreateTimer(async (source) =>
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        LvLevels.SelectedItem = LvLevels.Items[14];
                        LvLevels.ScrollIntoView(LvLevels.SelectedItem);
                    });
                }
                , period);

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 8:21 AM
    Moderator