0
votes

I have a question about setting vertical offset of scroll viewer in mvvm pattern. So my use case is like on a product list page, the user clicks the "load more" button at the bottom of the list, it gets more products from server and the page still has the same vertical offset when the user clicks "load more". I can get the vertical offset when the user clicks "load more", but I have problem with setting it after more products are retrieved from server. First I tried to bind the offset to a dependency property in view model, and update it in the view model. However, the system complains that the verticaloffset property of scrollviewer is readonly. I also tried to do it in the view. I want to invoke _scrollViewer.ScrollToVerticalOffset() when the listbox is updated. However, I couldn't find a event like Listbox_Updated in listbox. Any idea how should I do that? Thanks!

Fei

1
Why do you want to manipulate the vertical offset when load more happens?Filip Skakun

1 Answers

0
votes

You could create an attached dependency property (using DependencyProperty.RegisterAttached) that you set on a ListBox, which finds the ScrollViewer in the ListBox once it loads (perhaps using GetTemplateChild() or the VisualTreeHelper). The property could be bound to the view model and call ScrollToVerticalOffset when the value changes and update its value when LayoutUpdated event on the ScrollViewer gets raised.