WPF - 当 ItemsSource 更改时重置 ListBox 滚动位置
我当前有一个 ListBox,其 ItemsSource 集合绑定到我的视图模型上的 IEnumerable 类型的属性。当该 preoprty 的引用更改时,列表框会按预期更新,但是我遇到一个问题,如果我有大量项目集合并滚动到列表框的底部,然后将引用更改为包含 1 个项目的另一个集合,ListBox 视图为空白且不显示滚动条。然后我必须用鼠标滚轮向上滚动列表框,直到第 1 项进入视图。
因此,我认为我所追求的是一种每当 ItemsSource 属性发生更改时将 ListBox 的滚动位置重置到顶部的方法,这样无论集合有多大或多小,总是会显示某些内容。
I currently have a ListBox whose ItemsSource collection is bound to a property on my viewmodel, of type IEnumerable. When that preoprty's reference changes, the ListBox updates as expected, however I have a problem in that if I have a large collection of items and scroll to the bottom of the ListBox, and then change the reference to another collection containing, say, 1 item, the ListBox view is blank and no scrollbar is displayed. I have to then scroll the listbox up with the mouse wheel, until the 1 item comes into view.
So, what I think I'm after, is a way of resetting the scroll position of the ListBox to the top, whenever the ItemsSource property changes, so that something is always displayed no matter how large or small the collection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我无法重现您的问题(对我来说,更改
ItemsSource
时,ListBox
会滚动到新集合中的最后一项)。无论如何,要在每次ItemsSource
更改时将ListBox
滚动到顶部,您可以在后面使用一些代码。首先侦听ItemsSourceProperty
中的更改,然后在生成其项目后将ListBox
滚动到顶部更新
创建执行此操作的附加行为而是为了避免代码隐藏。可以像这样使用
ScrollToTopBehavior
以及 GetVisualChild 的实现
I'm unable to reproduce your problem (for me, the
ListBox
is scrolled to the last item in the new collection when changingItemsSource
). Anyway, to scroll theListBox
to the top every time itsItemsSource
changes you can use some code behind. First listen to changes in theItemsSourceProperty
and then scroll theListBox
to the top once its items has been generatedUpdate
Made an attached behavior that does this instead to avoid code behind. It can be used like this
ScrollToTopBehavior
And an implementation of GetVisualChild
迟到的答案:
一个简单的解决方案是为
TargetUpdated
事件添加事件处理程序,并在ItemsSource< 上设置
NotifyOnTargetUpdated=True
/code> 绑定:并在事件处理程序中,滚动到顶部项目:
Late answer:
A simple solution is to add an event handler for the
TargetUpdated
event, and setNotifyOnTargetUpdated=True
on theItemsSource
binding:and in the event handler, scroll to the top item:
试试这个:
Try this:
改进了 Fredrik Hedblad 的答案以使用 ObservableCollection:
Improved Fredrik Hedblad's answer to work with ObservableCollection:
设置控件格式时,选择一系列单元格作为选择选项,然后将其列在列表框中。您还可以选择一个单元格作为指向所选选项的链接,其中将根据列表中选项的位置显示数字。 1 代表列表中的第一个,2 代表第二个,等等。代码非常简单:-
Range("A1")Select
Selection = 1
Change ("A1") 到您链接的单元格
并将 1 更改为您要选择的列表中的位置。
作为链接的单元格引用是双向的 - 如果您更改选择,单元格中的数字会更改;如果您更改单元格中的数字,突出显示的选择也会更改。
When you format the control, you select a range of cells as the selection choices which are then listed in the list box. You also select a cell as the link to the selected choices in which a number will be displayed depending on the position of the selection in the list. 1 for first in the list, 2 for second etc. The code is quite simply:-
Range("A1")Select
Selection = 1
Change ("A1") to the cell you have linked
and change the 1 to the position in the list you want selected.
The cell reference being a link works both ways - if you change your selection, the number in the cell changes and if you change the number in the cell, the highlighted selection changes.