WPF列表视图 - 选择项目之外的“视野”
-
25-09-2019 - |
题
我用一个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;
}
这完全适用于项目,即处于excecution时可见。但对于项目,不可见的,ContainerFromIndex()返回null。听说这事做虚拟化,并且该列表不知道项目上升或下跌的“视野”。但如何来,它可以在列表中选择项目越位,当你手动选择“视野”?
和如何做一个选择的项目外的视场“的?我认为,必须是可能的。
感谢您的帮助, 标记
解决方案
正如你所说,我的猜测是,这个问题是ListView的项目的虚拟化。默认情况下,ListView控件(列表框和)使用VirtualizingStackPanel
作为其ItemsPanel以提高性能。它是如何工作的简要说明可以读取的此处。
可以然而改换另一面板。在这种情况下,请尝试使用正常的StackPanel。如果你有一吨在ListView的项目,特别是如果他们是复杂的项目,性能可能会降低一点。
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
修改强>
您也可以尝试的解决方案描述在这个类似的问题,这取决于你模型。然而,这可能不会对你的工作。
其他提示
当与虚拟项目的控件处理,以禁用虚拟化(这实际上是在时间非常有用的特性,即使它与API的其他部分的正确操作干扰)的替代,是找到VirtualizingPanel
和明确告诉它到滚动。
例如:
void ScrollToIndex(ListBox listBox, int index)
{
VirtualizingPanel panel = FindVisualChild<VirtualizingPanel>(listBox);
panel.BringIndexIntoViewPublic(index);
}
static T FindVisualChild<T>(DependencyObject o) where T : class
{
T result = o as T;
if (result != null)
{
return result;
}
int childCount = VisualTreeHelper.GetChildrenCount(o);
for (int i = 0; i < childCount; i++)
{
result = FindVisualChild<T>(VisualTreeHelper.GetChild(o, i));
if (result != null)
{
return result;
}
}
return null;
}
我不是很满意,需要通过视觉树搜索找到的面板,但我不知道任何其他方式得到它,也没有滚动到一个特定的指数与虚拟化面板打交道时
不隶属于 StackOverflow