0

I'm working on a UWP project with a ListView with items arranged horizontally. The ListView is right aligned and the first item added should always be visible even if any number of items are added afterwords.

I've successfully kept the first item to be shown when adding new items to the list, by using ScrollIntoView and setting the last item.

But it doesn't seem to work when first opening the app. It only scrolls to the middle of the list.

private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
   ItemsListView.SelectedItem = Items.Last();
   ItemsListView.ScrollIntoView(Items.Last(), ScrollIntoViewAlignment.Leading);
}

I also tried adding the ScrollIntoView inside the Loaded event of the ListView, tried with a ChangeView of a ScrollerView, and so on.

This is my ListView:

<ListView Name="ItemsListGrid"
                      Grid.Column="0"
                      HorizontalAlignment="Stretch" VerticalAlignment="Center"
                      BorderThickness="0"
                      Margin="0, 16, 24, 16"
                      ItemsSource="{x:Bind Items}"
                      ItemTemplateSelector="{StaticResource ItemTemplateSelector}"
                      ScrollViewer.HorizontalScrollMode="Enabled"
                      ScrollViewer.VerticalScrollMode="Disabled"
                      ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                      IsItemClickEnabled="True"
                      CanDragItems = "False"
                      CanDrag = "False"
                      CanReorderItems = "False"
                      AllowDrop = "False"
                      Loaded="Grid_Loaded">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid VerticalAlignment="Center" HorizontalAlignment="Right"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Margin" Value="0, 0, 0, 0"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

Another solution I've seen online is this:

            <ListView.ItemsPanel>  
                <ItemsPanelTemplate>  
                    <ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" VerticalAlignment="Bottom" />  
                </ItemsPanelTemplate>  
            </ListView.ItemsPanel> 

But I'm already setting the ItemsPanelTemplate inside my ListView and I can't use this.

Macaret
  • 797
  • 9
  • 33
  • When app is run and list is loaded it doesn't show the LAST ITEM in view but some random middle ITEM ? that's the problem ? – Hammas Apr 07 '20 at 15:18
  • check this https://stackoverflow.com/questions/32557216/windows-10-scrollintoview-is-not-scrolling-to-the-items-in-the-middle-of-a-lis – Hammas Apr 07 '20 at 15:28
  • Exactly as you described. Unfortunately I already tried all the suggested solutions from your link, and it isn't working. – Macaret Apr 08 '20 at 06:20
  • i think problem is with Virtualization of panel you may try using different container element .. – Hammas Apr 08 '20 at 07:47
  • Do you mean to use a Grid? – Macaret Apr 09 '20 at 08:10
  • @Macaret This may be related to virtualization. Is every item in your `ListView` highly consistent? You can explicitly set the `Height` of the item in the `ItemTemplate`. – Richard Zhang Apr 13 '20 at 01:38

0 Answers0