none
ListView ScrollInto Middle Of the view.

    Question

  • Hi,

    I have a ListView which contains around 500 items, I want to scroll a particulate item into view such that the scrolled item is shown in the middle of all visible items. ScrollIntoView() method scrolls the tems randomly anywhere in the view. Is there any way or hack available to mimic ScrollIntoMiddleOfView() ?

    Any help is appreciated,
    Thanks.
    - Naveen Dhaka
    Monday, June 16, 2008 9:57 AM

Answers

  • Well, I below code works for me:

    VirtualizingStackPanel vsp = (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic, null, _listView, null);

    double scrollHeight = vsp.ScrollOwner.ScrollableHeight;
    double offset = scrollHeight * itemIndex_ / _listView.Items.Count; // itemIndex_ is index of the item which we want to show in the middle of the view

    vsp.SetVerticalOffset(offset);


    Thanks to everybody who gave it a thought.
    - Naveen

    • Marked as answer by Marco Zhou Friday, June 20, 2008 10:15 AM
    Tuesday, June 17, 2008 7:01 AM

All replies

  • Well, I below code works for me:

    VirtualizingStackPanel vsp = (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic, null, _listView, null);

    double scrollHeight = vsp.ScrollOwner.ScrollableHeight;
    double offset = scrollHeight * itemIndex_ / _listView.Items.Count; // itemIndex_ is index of the item which we want to show in the middle of the view

    vsp.SetVerticalOffset(offset);


    Thanks to everybody who gave it a thought.
    - Naveen

    • Marked as answer by Marco Zhou Friday, June 20, 2008 10:15 AM
    Tuesday, June 17, 2008 7:01 AM
  • Hi Naveen,

    I have a scenario where, if am trying to use ListView.ScrollIntoView(anySelectedItem). in case the item is not in visible area, i am getting some kind of continues flickering List view.

    Then, i though of implementing the above mentioned method of resetting the visibility.

    But, i did't work well.

            private void ScrollIntoMiddleOfView(ListView _listView,int itemIndex_)
            {
                VirtualizingStackPanel vsp = (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.NonPublic, null, _listView, null);

                double scrollHeight = vsp.ScrollOwner.ScrollableHeight;
                double offset = scrollHeight * itemIndex_ / _listView.Items.Count; // itemIndex_ is index of the item which we want to show in the middle of the view

                vsp.SetVerticalOffset(offset);

            }

    I have a doubt on the highlighted part..
    Any help is appreciated.

    Warm regards
    Dinakaran.
    Monday, January 11, 2010 6:04 PM