Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF Listview-选择“视野”之外的项目_Wpf_Listview_Listviewitem_Itemcontainergenerator - Fatal编程技术网

WPF Listview-选择“视野”之外的项目

WPF Listview-选择“视野”之外的项目,wpf,listview,listviewitem,itemcontainergenerator,Wpf,Listview,Listviewitem,Itemcontainergenerator,我正在使用ListView显示列表中的项目。用户可以自己选择项目,或者使用一些“预选键”选择具有指定属性的项目 要检查项目,我使用类似的方法: for(int i;i<MyListView.Items.Count;++i) { if( /*... Check if the items should be selected ...*/ ) (MyListView.ItemContainerGenerator.ContainerFromIndex(i) as ListV

我正在使用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。我听说这与虚拟化有关,而且列表不知道“视野”中项目的上下两面。但是,当您手动选择列表中的选定项目时,为什么可能会使其偏离“视野”呢

以及如何选择“视野”之外的项目?我想那一定是可能的

谢谢你的帮助,
标记

正如您所提到的,我猜问题在于ListView项的虚拟化。默认情况下,ListView和ListBox使用VirtualzingStackPanel作为其ItemsPanel以提高性能。可以阅读有关其工作原理的简要说明

但是,您可以替换另一个面板。在这种情况下,请尝试使用普通StackPanel。如果ListView中有大量项,尤其是复杂项,性能可能会受到一些影响

<ListView>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
编辑


根据您的型号,您也可以尝试使用所描述的解决方案。但是,这可能对您不起作用。

在处理虚拟化项控件时,禁用虚拟化的另一种方法是找到虚拟化面板,并明确告诉它滚动。禁用虚拟化实际上有时是一种有用的功能,即使它会干扰API其他部分的正确操作

例如:

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;
}

我不太满意通过视觉树搜索面板的需要,但我不知道有任何其他方法可以获得它,也不知道在处理虚拟化面板时滚动到特定索引。

但是,当我选择一个项目时,滚动到看不见的地方并向后滚动,它仍然被选中,这怎么可能呢?这些信息必须存储在某个地方。这真的是不可访问的吗?嗯,所选项目可以通过SelectedItems访问。它返回一个IList,它有一个Add方法。是否尝试将要选择的项目添加到该列表中?这意味着您必须从ListView中显示的任何集合向此IList添加项。可能没有办法只使用索引。谢谢你的回答,这让我发疯了。