EDITED for clarity:
I have 2 ListView i my application. The first one contains a List < KeyPair< String, List< Elements > >. The display of the items in the list is templated to show only the Key (String). When the user selects an item in this lists (SelectionChanged), it changes the ItemSource of my second ListView to the Value of the Keypair.
This is placed inside a Grid with a Column width of Auto.
<ListView ItemsSource="{Binding Path=FunctionIndex.Index}"
Name="completeFunctionIndexView"
Style="{StaticResource SearchListStyle}"
SelectionChanged="functionIndexView_SelectionChanged" />
<ListView Name="SearchResults"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ListView.View>
<GridView >
<GridViewColumn
CellTemplate="{StaticResource ElementLV}"
Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
The Style SearchListStyle only constains a TextBlock showing the Key (since it contains KeyPairs). ElementLV is a small Text block with Runs bound to diverse properties and ToString values of my elements.
private void functionIndexView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This supposes we want to support a "multiple selection search"
//for now this isn't implemented
List<Element> Results = new List<Element>();
foreach (KeyValuePair<String, List<Element>> item in e.AddedItems)
{
Results = item.Value;
break;
}
SearchResults.ItemsSource = Results;
}
These list which can be displayed in the SearchResults listview can have large number of elements (100k+). I've put the IsVirtualizing on the second listview, and most of the time it works like charm.
But sometimes, I can't quite put my finger on what causes it (too quick a click to select an item maybe? ) the application decides it must generates all the items in the list (which is not wanted obviously)...
Can someone point me in the right direction? Is there a reason why the SearchResults would decide to ask to generate all items?
Could it be linked to the Width of the items and the SearchResults ListView, when Measuring, trying to measure ALL its items?