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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4065 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.