Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 829963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:57:56+00:00 2026-05-15T03:57:56+00:00

ListView.ScrollIntoView(object) currently finds an object in the ListView and scrolls to it. If you

  • 0

ListView.ScrollIntoView(object) currently finds an object in the ListView and scrolls to it. If you are positioned beneath the object you are scrolling to, it scrolls the object to the top row. If you are positioned above, it scrolls it into view at the bottom row.

I’d like to have the item be scrolled right into the center of my list view if it is currently not visible. Is there an easy way to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:57:57+00:00Added an answer on May 15, 2026 at 3:57 am

    It is very easy to do this in WPF with an extension method I wrote. All you have to do to scroll an item to the center of the view is to call a single method.

    Suppose you have this XAML:

    <ListView x:Name="view" ItemsSource="{Binding Data}" /> 
    <ComboBox x:Name="box"  ItemsSource="{Binding Data}"
                            SelectionChanged="ScrollIntoView" /> 
    

    Your ScrollIntoView method will be simply:

    private void ScrollIntoView(object sender, SelectionChangedEventArgs e)
    {
      view.ScrollToCenterOfView(box.SelectedItem);
    } 
    

    Obviously this could be done using a ViewModel as well rather than referencing the controls explicitly.

    Following is the implementation. It is very general, handling all the IScrollInfo possibilities. It works with ListBox or any other ItemsControl, and works with any panel including StackPanel, VirtualizingStackPanel, WrapPanel, DockPanel, Canvas, Grid, etc.

    Just put this in a .cs file somewhere in your project:

    public static class ItemsControlExtensions
    {
      public static void ScrollToCenterOfView(this ItemsControl itemsControl, object item)
      {
        // Scroll immediately if possible
        if(!itemsControl.TryScrollToCenterOfView(item))
        {
          // Otherwise wait until everything is loaded, then scroll
          if(itemsControl is ListBox) ((ListBox)itemsControl).ScrollIntoView(item);
          itemsControl.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
            {
              itemsControl.TryScrollToCenterOfView(item);
            }));
        }
      }
    
      private static bool TryScrollToCenterOfView(this ItemsControl itemsControl, object item)
      {
        // Find the container
        var container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
        if(container==null) return false;
    
        // Find the ScrollContentPresenter
        ScrollContentPresenter presenter = null;
        for(Visual vis = container; vis!=null && vis!=itemsControl; vis = VisualTreeHelper.GetParent(vis) as Visual)
          if((presenter = vis as ScrollContentPresenter)!=null)
            break;
        if(presenter==null) return false;
    
        // Find the IScrollInfo
        var scrollInfo = 
            !presenter.CanContentScroll ? presenter :
            presenter.Content as IScrollInfo ??
            FirstVisualChild(presenter.Content as ItemsPresenter) as IScrollInfo ??
            presenter;
    
        // Compute the center point of the container relative to the scrollInfo
        Size size = container.RenderSize;
        Point center = container.TransformToAncestor((Visual)scrollInfo).Transform(new Point(size.Width/2, size.Height/2));
        center.Y += scrollInfo.VerticalOffset;
        center.X += scrollInfo.HorizontalOffset;
    
        // Adjust for logical scrolling
        if(scrollInfo is StackPanel || scrollInfo is VirtualizingStackPanel)
        {
          double logicalCenter = itemsControl.ItemContainerGenerator.IndexFromContainer(container) + 0.5;
          Orientation orientation = scrollInfo is StackPanel ? ((StackPanel)scrollInfo).Orientation : ((VirtualizingStackPanel)scrollInfo).Orientation;
          if(orientation==Orientation.Horizontal)
            center.X = logicalCenter;
          else
            center.Y = logicalCenter;
        }
    
        // Scroll the center of the container to the center of the viewport
        if(scrollInfo.CanVerticallyScroll) scrollInfo.SetVerticalOffset(CenteringOffset(center.Y, scrollInfo.ViewportHeight, scrollInfo.ExtentHeight));
        if(scrollInfo.CanHorizontallyScroll) scrollInfo.SetHorizontalOffset(CenteringOffset(center.X, scrollInfo.ViewportWidth, scrollInfo.ExtentWidth));
        return true;
      }
    
      private static double CenteringOffset(double center, double viewport, double extent)
      {
        return Math.Min(extent - viewport, Math.Max(0, center - viewport/2));
      }
      private static DependencyObject FirstVisualChild(Visual visual)
      {
        if(visual==null) return null;
        if(VisualTreeHelper.GetChildrenCount(visual)==0) return null;
        return VisualTreeHelper.GetChild(visual, 0);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In WPF, I know I can use ListView.ScrollIntoView to scroll a particular item into
My listview row appears with one word name. I want to change into two
The ListView has a nice fade on the top/bottom. I have a ListView in
My Listview is setup in the details view with the following column headers: Image
$(#listView object.modal).click(function(){ // Get the ID of the clicked link: var link = $(this).closest(h2).attr(title);
A ListView with Datatemplate in GridViewColumn: <ListView Name =LogDataList IsSynchronizedWithCurrentItem=True ItemsSource={Binding LogDataCollection} Background=Cyan> <ListView.View>
My Listview control contains 4 columns and 30 rows. I can retrieve the row
In listview im showing product information .On each row there is one select button
I have a ListView that contains 3 checkboxes per row. I want to set
listView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.