I'm working on a UWP Windows 10 app. It contains a listview which redirects you to another page when an item is tapped I'm trying to get the last item to be re-selected when I navigate back to the previous page.
My listview's ItemSource is binded to an ObservableCollection
and loads my items as expected but when I navigate back, it keeps the item selected but it's not visible.
I've read various articles:
Make ListView.ScrollIntoView Scroll the Item into the Center of the ListView
ListViewBase.ScrollIntoView methods
Windows 10 ScrollIntoView() is not scrolling to the items in the middle of a listview
And quite a few others, and I tried to use the extension from the last article mentioned above which provided various extensions to the Listview
and I thought I'd be good to go but to no avail!
While investigating and trying various things out, I thought I'd make a call from
protected async override void OnNavigatedTo(NavigationEventArgs e)
and call the following code:
if (e.NavigationMode == NavigationMode.Back)
{
if (GetViewModel.SelectedChannel != null)
{
await this.lvwChannels.ScrollToIndex
(GetViewModel.SelectedIndex);
}
}
or
if (e.NavigationMode == NavigationMode.Back)
{
if (GetViewModel.SelectedChannel != null)
{
await this.lvwChannels.ScrollToItem
(GetViewModel.SelectedItem);
}
}
but it still not working.
This is when I noticed that my Listview.Items.count was returning 0 and yet the item are reloaded, but I assume they are being reloaded at a later stage so my question is:
but I noticed the following:
When stepping through the extension, the
public async static Task ScrollToIndex(this ListViewBase listViewBase, int index)
my listViewBase.Items.Count
is 0?? Why?
When trying to find the
ScrollViewer
within the ListView, it doesn't find it?When calling
var selectorItem = listViewBase.ContainerFromIndex(index) as SelectorItem;
selectorItem
is null as it doesn't find anything based on the index. The index is the only thing that appears to be set correct i.e. 10, 23, 37
Can someone point me the right direction? I assume it's a timing issue and that my ObservableCollection hasn't been re-binded at this stage. I could re-bind at just before calling the ScrollIntoView, but that's still not going to resolve the fact that my listViewBase is null.
Any ideas?
Thanks.