Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll WPF Listview to specific line

Tags:

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?

like image 382
Sam Avatar asked Oct 17 '08 12:10

Sam


1 Answers

Someone told me an even better way to scroll to a specific line, which is easy and works like charm.
In short:

public void ScrollToLastItem() {   lv.SelectedItem = lv.Items.GetItemAt(rows.Count - 1);   lv.ScrollIntoView(lv.SelectedItem);   ListViewItem item = lv.ItemContainerGenerator.ContainerFromItem(lv.SelectedItem) as ListViewItem;   item.Focus(); } 

The longer version in MSDN forums:

like image 54
Sam Avatar answered Oct 13 '22 01:10

Sam