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.