locked
Scroll ListView to the top (reset) RRS feed

  • Question

  • I'd like to scroll a ListView to the top (set the position of the scrollbar to zero).

    Why: Say I've got a ListView containing 10000 items. Now I want to clear the very same ListView and fill it with 100 Items.

    If the user scrolled down to the bottom of the ListView, only the last of the 100 items will be shown, and paging does not work very well for a few PgUps since the Scrollbar size still is set for 10000 items.

    So I'd like to reset the ListView to position zero when I refill it. I tried to

    if (MyListview.Items.Count>0)

      MyListview.ScrollIntoView(MyListview.Items[0]);

     

    but it did not help, scroll position still was down :(

    thanks,

    Sam

    Thursday, January 25, 2007 10:51 AM

All replies

  • (((VisualTreeHelper.GetChild(MyListview,0) as Border).Child) as ScrollViewer).ScrollToVerticalOffset(0);

    Thursday, January 25, 2007 1:16 PM
  • Nice.

    Well, at first this gave me a null reference exception. Since this exception does not help much I changed the code to get a more useful exception:

    (((ScrollViewer)((Border)VisualTreeHelper.GetChild(MyListview, 0)).Child)).ScrollToVerticalOffset(0);

    this got me the exception "Das Objekt des Typs Microsoft.Windows.Themes.ListBoxChrome kann nicht in Typ System.Windows.Controls.Border umgewandelt werden."

    So it can be something different than a border, well, I just walked up the inheritance tree, and this worked, both Border and ListBoxChrome inherit from Decorator:

    (((ScrollViewer)((Decorator)VisualTreeHelper.GetChild(DatenListview, 0)).Child)).ScrollToVerticalOffset(0);

    This does work, but can I trust it? Next update, another windows style or version, a lot of things may break this hack.

    Ain't there some way to achieve this using defined APIs instead of the rather murky visual tree?

    (thanks, though, for now I'll just wrap an exception handler around this)

    Sam

    Friday, January 26, 2007 9:47 AM
  • I dont know of any other way to get the scrollviewer. whenever we use visualtree and cast it like this we might run into these issues, because the Templates of the controls might be different. instead of using getchild at a parrticular position it is better to walk up or down tthe tree until we find the element.(it is done sameway here also http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=989561&SiteID=1)

    Friday, January 26, 2007 9:57 AM
  •  MyListview.ScrollIntoView(MyListview.Items[0]); should work.  i was playing around with it a bit and works for me.  Do you have a larger code sample that could be looked at. 
    Friday, January 26, 2007 4:24 PM
  • yes ScrollIntoView does work. I think for some reason Sam is getting 'null' for the item and that is the reason it is not working
    Friday, January 26, 2007 4:36 PM
  • How is the ListView being cleared of the 10000 items?  Once repopulated, it should have item[0] in view.
    Friday, January 26, 2007 7:53 PM
  • Matt,

    I clear the ListView like this (it is bound to DataContext):

    MyListview.DataContext = new ObservableCollection<SomeDataType>();

    I have not tried to clear the collection so far, could that be the problem?

    Sam

    • Proposed as answer by Naveen Dhaka Tuesday, June 17, 2008 7:03 AM
    Monday, January 29, 2007 11:44 AM
  • Hi I have a list view and ScrollIntoView(object) works for me. but my use case is a bit different. I want to show the specified item in the middle of the list view. (Currently it scrolls to the item and shows it on the top.)Is there any way to do it?
    Monday, June 16, 2008 9:01 AM
  • If performance in not an issue below solution can be tried:

    http://forums.msdn.microsoft.com/en-US/wpf/thread/7c5812bd-482f-448c-bc67-02573e48d691/
    Tuesday, June 17, 2008 7:03 AM