WPF – WP7 – Rolling Listbox in external ScrollViewer

I have the following page layout in my application:

 Grid.Row="1">

MaxHeight="600"
VerticalAlignment="Top"
HorizontalAlignment= "Stretch">













































Me Added an external ScrollViewer instead of using the ListBox, because without it, the things on the ListBox take up too much space without leaving enough space to view the ListBox content. Now, the problem is if I am in the ListBox Add an item to the end of the ScrollIntoView method does not work. So I need to use ScrollViewer’s ScrollToVerticalOffset method.

When the user clicks the button on the application bar, I add the new item to the ListBox bound to ObservableCollection. How to calculate the value to be passed to ScrollViewer.ScrollToVerticalOffset?

Thank you for your help!

You can find the container generated by the ListBox to host your elements. Once you have this container, you can Find the position relative to the scrollviewer:

var newItem = // the item you just added to your listbox

// find the ListBox container
listBox.UpdateLayout()
var element = listBox.ItemContainerGenerator.ContainerFromItem(newItem) as FrameworkElement;

// find its position in the scroll viewer
var transform = element.TransformToVisual(ScrollViewer);
var elementLocation = transform.Transform(new Point(0, 0));
double newVerticalOffset = elementLocation.Y + ScrollViewer.VerticalOffset;

// scroll into view
ScrollViewer.ScrollToVerticalOffset(newVerticalOffset);

I hope it helps

There are The following page layout:

 Grid.Row="1">

MaxHeight="600"
VerticalAlignment="Top"
HorizontalAlignment="Stretch">
























< ItemsPresenter />






< !-- .... -->











< /ScrollViewer>

I added an external ScrollViewer instead of using a ListBox, because without it, the things on the ListBox take up too much space and nothing is left Enough space to view the ListBox content.

Now, the problem is that if I add an item to the end of the ListBox, the ScrollIntoView method does not work. So I need to use the ScrollViewer’s ScrollToVerticalOffset method.

When the user clicks the button on the app bar, I add a new item to the ObservableCollection bound to the ListBox. How to calculate the value to be passed to ScrollViewer.ScrollToVerticalOffset?

Thank you for your help!

You can find the container generated by the ListBox to host your elements. After you have this container, you can find the position relative to the scrollviewer:

var newItem = // the item you just added to your listbox

// find the ListBox container
listBox.UpdateLayout()
var element = listBox.ItemContainerGenerator.ContainerFromItem(newItem) as FrameworkElement;

// find its position in the scroll viewer
var transform = element.TransformToVisual(ScrollViewer);
var elementLocation = transform.Transform(new Point(0, 0));
double newVerticalOffset = elementLocation.Y + ScrollViewer.VerticalOffset;

// scroll into view
ScrollViewer .ScrollToVerticalOffset(newVerticalOffset);

I hope it helps

Leave a Comment

Your email address will not be published.