WPF, Browserlike app.
I got one page containing a ListView. After calling a PageFunction I add a line to the ListView, and want to scroll the new line into view:
ListViewItem item = ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
if (item != null)
ScrollIntoView(item);
This works. As long as the new line is in view the line gets the focus like it should.
Problem is, things don't work when the line is not visible.
If the line is not visible, there is no ListViewItem for the line generated, so ItemContainerGenerator.ContainerFromIndex returns null.
But without the item, how do I scroll the line into view? Is there any way to scroll to the last line (or anywhere) without needing an ListViewItem?
Thanks for that last tip Sam. I had a dialog which opened, meaning my grid lost focus every time the dialog closed. I use this:
I made some changes to Sam's answer. Note that I wanted to scroll to the last line. Unfortunately the ListView sometiems just displayed the last line (even when there were e.g. 100 lines above it), so this is how I fixed that:
Cheers
To overcome the virtualisation issue but still use
ScrollIntoView
and not hacking around in the guts of the ListView, you could also use your ViewModel objects to determine what is selected. Assuming that you have ViewModel objects in your list that feature anIsSelected
property. You'd link the items to the ListView in XAML like this:Then, the code-behind method can scroll to the first selected item with this:
This also works if the selected item is well out of view. In my experiment, the
PersonsListView.SelectedItem
property wasnull
, but of course your ViewModelIsSelected
property is always there. Be sure to call this method after all binding and loading has completed (with the rightDispatcherPriority
).Using the ViewCommand pattern, your ViewModel code could look like this:
I think the problem here is that the ListViewItem is not created yet if the line is not visible. WPF creates the Visible on demand.
So in this case you probably get
null
for the item, do you? (According to your comment, you do)I have found a link on MSDN forums that suggest accessing the Scrollviewer directly in order to scroll. To me the solution presented there looks very much like a hack, but you can decide for yourself.
Here is the code snippet from the link above:
In my project i need to display the selected index line from the listview to the user so i assigned the selected item to ListView control. This code will scroll the scrollbar and display the selected item.
BooleanListView.ScrollIntoView(BooleanListView.SelectedItem);
OR
var listView = BooleanListView; listView.SelectedItem = listView.Items.GetItemAt(BooleanListView.SelectedIndex); listView.ScrollIntoView(listView.Items[0]); listView.ScrollIntoView(listView.SelectedItem);
If you just want to show and focus the last item after creating a new data item, this method is maybe better. Compare to ScrollIntoView, ScrollToEnd of ScrollViewer is in my tests more reliable. In some tests using ScrollIntoView method of ListView like above failed and i don't know reason. But using ScrollViewer to scroll to last one can work.