开发者

Preserving a bound ListBox's scroll position when the underlying collection changes

开发者 https://www.devze.com 2023-03-15 22:19 出处:网络
I have a ScrollViewer and a ListBox inside it which is bound to an ObservableCol开发者_运维问答lection in the view model. The ScrollViewer is maximized to take up all available space of the parent con

I have a ScrollViewer and a ListBox inside it which is bound to an ObservableCol开发者_运维问答lection in the view model. The ScrollViewer is maximized to take up all available space of the parent container. I'm finding that when the collection is modified and ends up producing more ListBoxItems than can fit in the viewable area of the ScrollViewer, the ScrollViewer scrolls down to show the last item in the ListBox. How do I prevent the ScrollViewer from scrolling when the child ListBox's items are updated? I would like the scroll position to stay intact whenever the collection in the view model is updated. Thanks in advance!


You are going to have to manage this yourself. The ListBox has a ScrollIntoView method that allows you to scroll to a specific location:

http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(v=VS.95).aspx

Determining the items that are currently visible, if you need this, is not so easy. See the ItemsControlExtensions that I wrote as part of the WP7Contrib project:

http://wp7contrib.codeplex.com/SourceControl/changeset/view/67473#1475881

This has a GetItemsInView extensions method that will provide the list of visible items.

0

精彩评论

暂无评论...
验证码 换一张
取 消