wpflistviewlistviewitemitemcontainergenerator

WPF Listview - Select items outside 'field of view'


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


Solution

  • 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.

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

    Edit

    You can also try to solution described at this similar question, depending on your model. However, this probably won't work for you.