---
title: "Listbox ScrollIntoView doesn't work for variable height items"  
description: "Listbox ScrollIntoView doesn't work for variable height items"  
author: "Anonymous User"  
published: 2013-09-21  
updated: 2013-09-21  
canonical: https://www.mindstick.com/forum/1464/listbox-scrollintoview-doesn-t-work-for-variable-height-items  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# Listbox ScrollIntoView doesn't work for variable height items

I have a [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) that various items are added to. When a new item is added to the listbox, I need to scroll that item into view (basically scroll to the bottom).

I've tried the solution from WPF ListBox Scroll when item added and also from this [blog post](https://answers.mindstick.com/qa/103651/what-is-a-blog-post)

However, neither [solutions](https://www.mindstick.com/articles/12807/product-searching-issues-and-solutions-for-ecommerce-store-advanced-search-autocomplete-suggest) work because my listbox contains [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) height items. If I hack my listbox items [templates](https://www.mindstick.com/articles/12874/crucial-benefits-of-using-powerpoint-templates-in-your-business-presentations) to have a fixed height instead, then it seems to work. Here is an example of one of my item templates:

<DataTemplate x:Key="StatusMessageTemplate">

<Grid Grid.Column="1" VerticalAlignment="top" [Margin](https://answers.mindstick.com/qa/104990/what-is-a-margin-call)="0,5,10,0">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="20"></RowDefinition>

</Grid.RowDefinitions>

<TextBlock Text="{[Binding](https://www.mindstick.com/blog/146/dynamic-binding-in-c-sharp) Path=MessageText}" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" FontWeight="Bold" Foreground="{DynamicResource LightTextColorBrush}"/>

<TextBlock Text="{Binding Path=created_at, StringFormat=t}" [Style](https://www.mindstick.com/articles/126300/apa-citation-style-get-to-know-about-it-for-your-next-assignment)="{StaticResource [Timestamp](https://www.mindstick.com/interview/791/what-does-timestamp-on-update-current_timestamp-data-type-do)}" TextWrapping="Wrap" HorizontalAlignment="[Right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not)" Grid.Row="0" Grid.Column="1"/>

</Grid>

</DataTemplate>

How can I make the new items scroll into view regardless of their height?

## Replies

### Reply by Sumit Kesarwani

Hi JayPrakash,\

I think I have found the problem. Variable height items are not calculated until displayed. So I add a timer to call the ScrollIntoView function. But even that didn't work well, so I used the VisualTreeHelper to find the ScrollViewer object and force it to the specific row. Here is the code.

```
System.Windows.Threading.DispatcherTimer dTimer = new
System.Windows.Threading.DispatcherTimer();     dTimer.Interval =
new TimeSpan(0, 0, 0, 0, 200); // 200 Milliseconds     dTimer.Tick +=
new EventHandler(        (seder, ea)
=>        {          
//Verses.ScrollIntoView(Verses.Items[itemIndex]);           for (int i
= 0; i < VisualTreeHelper.GetChildrenCount(Verses); i++)           {             
DependencyObject depObj = VisualTreeHelper.GetChild(Verses, i);              if
(depObj is ScrollViewer)              {                
ScrollViewer sv = depObj as ScrollViewer;                
sv.ScrollToVerticalOffset(itemIndex); // Zero based index                
break;              }           }          
dTimer.Stop();        });     dTimer.Start();
```


---

Original Source: https://www.mindstick.com/forum/1464/listbox-scrollintoview-doesn-t-work-for-variable-height-items

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
