当底层集合更改时保留绑定的 ListBox 的滚动位置
我里面有一个 ScrollViewer 和一个 ListBox,它绑定到视图模型中的 ObservableCollection。 ScrollViewer 最大化以占据父容器的所有可用空间。我发现当集合被修改并最终产生的 ListBoxItems 数量超出了 ScrollViewer 可视区域的容纳范围时,ScrollViewer 会向下滚动以显示 ListBox 中的最后一项。当子 ListBox 的项目更新时,如何防止 ScrollViewer 滚动? 我希望每当更新视图模型中的集合时滚动位置保持不变。 提前致谢!
I have a ScrollViewer and a ListBox inside it which is bound to an ObservableCollection 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须自己处理这个问题。
ListBox
有一个ScrollIntoView
方法,允许您滚动到特定位置:http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(v=VS.95).aspx
确定当前的项目可见,如果你需要这个,并不是那么容易。请参阅我作为 WP7Contrib 项目的一部分编写的
ItemsControlExtensions
:http://wp7contrib.codeplex.com/SourceControl/changeset/view/67473#1475881
这有
GetItemsInView
扩展方法将提供可见项目的列表。You are going to have to manage this yourself. The
ListBox
has aScrollIntoView
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.