WPF 列表视图 - 选择“视野”之外的项目
我正在使用 ListView 显示列表中的项目。用户可以自己选择项目,或者使用一些“预选键”来选择具有指定属性的项目。
要检查项目,我使用类似的东西:
for(int i;i<MyListView.Items.Count;++i)
{
if( /*... Check if the items should be selected ...*/ )
(MyListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem).IsSelected = true;
}
这对于执行时可见的项目非常有效。但对于不可见的项目,ContainerFromIndex() 返回 null。我听说这与虚拟化有关,并且列表不知道“视野”上方或下方的项目。但是,当您手动选择列表中的项目时,为什么可能会超出“视野”呢?
以及如何选择“视野”之外的项目?我认为这一定是可能的。
感谢您的帮助, 马克
I'm using a ListView to show Items in a List. The user can select the items himself, or use some 'preselect keys' to select items with specified attributes.
To check the items I use something like that:
for(int i;i<MyListView.Items.Count;++i)
{
if( /*... Check if the items should be selected ...*/ )
(MyListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem).IsSelected = true;
}
This works perfectly for items, that are visible at the time of excecution. But for items, that are not visible, ContainerFromIndex() returns null. I heard this has something to do with virtualization, and that the List doesn't know about items upside or downside the 'field of view'. But how comes that it is possible to have selected items in the List offside the 'field of view' when you select them manually?
And how to do a select of an item outside 'field of view'? I think that must be possible.
Thanks for any help,
Marks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所提到的,我的猜测是问题是 ListView 项目的虚拟化。默认情况下,ListView(和 ListBox)使用 VirtualizingStackPanel 作为其 ItemsPanel 来提高性能。有关其工作原理的简要说明,请阅读 此处。
不过,您可以替换另一个面板。在这种情况下,请尝试使用普通的 StackPanel。如果 ListView 中有大量项目,特别是复杂的项目,性能可能会受到一些影响。
编辑
您还可以尝试此类似问题,取决于您的型号。但是,这可能对您不起作用。
As you mentioned, my guess is that the problem is virtualization of the ListView items. By default, ListView (and ListBox) use
VirtualizingStackPanel
as their ItemsPanel to improve performance. A brief explanation for how it works can be read here.You can substitute another Panel, however. In this case, try using a normal StackPanel. If you have a ton of items in the ListView, especially if they are complex items, performance might suffer a little.
Edit
You can also try to solution described at this similar question, depending on your model. However, this probably won't work for you.
在处理虚拟化项目控件时,禁用虚拟化的替代方法(实际上,有时这是一个有用的功能,即使它会干扰 API 其他部分的正确操作),就是找到
VirtualizingPanel
并明确告诉它滚动。例如:
我不太满意需要通过视觉树搜索来查找面板,但我不知道有任何其他方式来获取它,也不知道在处理虚拟化面板时滚动到特定索引。
When dealing with virtualizing items controls, an alternative to disabling virtualization (which is in fact a useful feature at times, even if it interferes with correct operation of other parts of the API), is to find the
VirtualizingPanel
and tell it explicitly to scroll.For example:
I'm not very happy with the need to search through the visual tree to find the panel, but I'm not aware of any other way to get it, nor to scroll to a specific index when dealing with a virtualizing panel.