1

When I use ScrollIntoView for my listview, The page goes blank for a moment and then rerenders, how can I make this more smooth?

Here is my current code:

<ListView Name="MessageList" Margin="0,82,0,45"
                  SelectionMode="None"
                  IsItemClickEnabled="True"
                  ItemClick="FileMessage_Click"
                  HorizontalAlignment="Stretch"
                  ItemTemplateSelector="{StaticResource MsgDataTemplateSelector}"
                  ItemsSource="{x:Bind messages}" >

         <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
         </ListView.ItemContainerStyle>

</ListView>

And in the code-behind I have this each time a new message is added to the observable collection that feeds into the listview:

MessageList?.ScrollIntoView(MessageList.Items[MessageList.Items.Count -1]);
Jenny
  • 85
  • 1
  • 8
  • Are you using virtualization on something? – Chris W. Jul 20 '16 at 17:45
  • I haven't added any virtualization, is that automatic anywhere? – Jenny Jul 20 '16 at 17:49
  • yes, it's automatic. – AlexDrenea Jul 20 '16 at 17:50
  • How do I turn it off? – Jenny Jul 20 '16 at 17:50
  • Attached property `VirtualizingStackPanel.IsVirtualizing="False"` or at least that's a way, UWP not sure, haven't gotten to play with it enough yet. Worse case scenario just override the `ListView.ItemsPanel` – Chris W. Jul 20 '16 at 17:53
  • Like the second part of [this](http://stackoverflow.com/questions/23371810/wpf-listview-virtualization-how-to-disable-listview-virtualization) should do. Keep in mind the potential performance hit though if you're loading tons of stuff in there. – Chris W. Jul 20 '16 at 17:54
  • For some reason I don't have a VirtualizationMode property of VirtualizingStackPanel as is shown in that answer, is this an old way to do it? – Jenny Jul 20 '16 at 19:41
  • That attached property may be a WPF only thing, but changing the ItemsPanel in the second example should port right over to UWP fine. – Chris W. Jul 20 '16 at 20:57
  • Will that work if I'm using a data template selector? – Jenny Jul 20 '16 at 21:19
  • When I do that, it doesn't automatically scroll to the bottom anymore. – Jenny Jul 20 '16 at 21:26

1 Answers1

0

I didn't reproduced your problem completely. On my PC, this problem only occurs at the first time I add item to ListView. ListView use ItemsStackPanel as its default panel, which supports UI Virtualization. After changing the default panel to StackPanel which has no support for UI Virtualization, the problem doesn't appear.

You can change the ItemsPanel of ListView like below:

<ListView Name="MessageList" Margin="0,82,0,45"
              SelectionMode="None"
              Height="300"
              IsItemClickEnabled="True"
              HorizontalAlignment="Stretch"
              ItemsSource="{x:Bind messages}" >

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel></StackPanel>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>

</ListView>

Update: To scroll to the last Item, you need to set the height of ListView and call ListView.UpdateLayout before ScrollIntoView:

 MessageList.UpdateLayout();
 MessageList.ScrollIntoView(MessageList.Items[MessageList.Items.Count - 1]);
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • This doesn't scroll me to the bottom automatically, thats the problem – Jenny Jul 25 '16 at 17:41
  • I am also using a data template selector for 5 different data templates, does that effect it? – Jenny Jul 25 '16 at 17:45
  • You need to set the height of ListView and call Update before `ScrollIntoView`. Please see my Update. – Elvis Xia - MSFT Jul 26 '16 at 09:34
  • That solved most of the problem but now it scrolls to show the one message immediately before the most recent message. Why doesn't it scroll all the way down? – Jenny Jul 26 '16 at 21:49
  • I think it is possibly by design. If there are 10000 or more items. Scrolling all the way down would keep user waiting. – Elvis Xia - MSFT Jul 27 '16 at 08:57