none
WPF ListBox VirtualizingStackPanel.VirtualizationMode=“Recycling” causes same list items to always appear when scrolling

    Question

  • I'm using WPF/.NET 4 and when I add the VirtualizingStackPanel.VirtualizationMode="Recycling" property to my ListBoxes, it causes the same list items to be repeated over and over when scrolling. For example, let's say I have a ListBox with 100 items and 10 are visible at a time. When the ListBox first loads I see items 1 - 10 like I should, but when I start scrolling down to see the remaining items 11 - 100, items 1 - 10 are repeated over and over as I scroll down, so I'm never able to view items 11 - 100. If I change Recycling back to Standard, then everything works correctly, but the scrolling is horribly slow (my ListItems are fairly complex, with several images, text fields, and buttons in them). When Recycling is on, the scrolling is super fast, but I can only ever view items 1 - 10. Any ideas on what's wrong or how to fix it? Thanks in advance.
    - Dan - "Can't never could do anything"
    Friday, February 04, 2011 3:53 AM

All replies

  • Hi ,

    try to set this properties

     <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
    ScrollViewer.IsDeferredScrollingEnabled will improve scrolling
    Friday, February 04, 2011 4:30 AM
  • I tried adding the ScrollViewer.IsDeferredScrollingEnabled="True" as well, but still have the same result.  Here's how I have tried setting the properties on my listboxes:

    <ListBox HorizontalContentAlignment="Stretch" 
    ItemContainerStyle="{StaticResource GlowContainer}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    VerticalAlignment="Top" 
    Visibility="Collapsed" 
    x:Name="stkReportList" 
    Background="Transparent" 
    BorderBrush="#BBBBBB" 
    BorderThickness="1,1,1,0" 
    VirtualizingStackPanel.IsVirtualizing="True" 
    VirtualizingStackPanel.VirtualizationMode="Recycling" 
    ScrollViewer.IsDeferredScrollingEnabled="True">
    ...
    </Listbox>
    
    and

    <ListBox HorizontalContentAlignment="Stretch" 
    ItemContainerStyle="{StaticResource GlowContainer}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    VerticalAlignment="Top" 
    Visibility="Collapsed" 
    x:Name="stkReportList" 
    Background="Transparent" 
    BorderBrush="#BBBBBB" 
    BorderThickness="1,1,1,0" 
    Style="{DynamicResource ListBoxStyle}">
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">			
    <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>			
    <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>			
    <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>		
    </Style>
    ...
    </ListBox>
    
    I am then setting the ListBox's ItemsSource from code-behind to actually show items in the listbox.  Any other suggestions?  Thanks.


    - Dan - "Can't never could do anything"
    Monday, February 07, 2011 8:03 PM
  • Also, I would prefer not to use deferred scrolling if possible, as I want the contents of the listbox to scroll without users having to release the mouse button from the scroll bar.
    - Dan - "Can't never could do anything"
    Monday, February 07, 2011 8:19 PM
  • I should mention too that I am using a DataTemplate for my ListBoxes as well, which is displaying the ListBoxItems as a custom UserControl.  Here is an example of how one of my ListBoxes that uses data binding looks:

    <ListBox x:Name="lstInvoiceItems" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    ItemsSource="{Binding Path=SaleInvoiceModel.Products}" 
    ItemContainerStyle="{StaticResource InvoiceItemsListBoxItem}" 
    KeyboardNavigation.TabNavigation="Continue" 
    HorizontalContentAlignment="Stretch" Background="Transparent"
     Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count, Converter={StaticResource VisibilityByCountConverter}}" 
    VirtualizingStackPanel.IsVirtualizing="True">
    	<ListBox.ItemTemplate>
    		<DataTemplate>
    			<controls2:InvoiceItemControl x:Name="InvoiceItem" InvoiceItem="{Binding}" Loaded="InvoiceItem_Loaded" UpdatedValue="InvoiceItem_UpdatedValue" ItemSelected="InvoiceItem_ItemSelected" RemoveItem="InvoiceItem_RemoveItem" ShowRelatedProducts="InvoiceItem_ShowRelatedProducts" EnableContextMenu="true" GoToProductClicked="InvoiceItem_GoToProductClicked"  />
    		</DataTemplate>
    	</ListBox.ItemTemplate>
    </ListBox>
    


    - Dan - "Can't never could do anything"
    Tuesday, February 08, 2011 7:48 PM
  • your template must make use of ItemsPresenter, because ItemsPresenter offers you the Virtualization by the VirtualizingStackPanel

    <ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" ...>
     <ListBox.Template>
      <ControlTemplate>
      <ScrollViewer ..>
        <ItemsPresenter VirtualizingStackPanel.VirtualizationMode=
         "{TemplateBinding VirtualizingStackPanel.VirtualizationMode}" .../>
      </ScrollViewer>
    ....
    

    certainly you will put it inside a border, grid, scrollviewr ...

     


    Alan-SY

     

    Tuesday, February 08, 2011 8:10 PM
  • Tried this, but it didn't seem to help.  We did manage to get one of our ListBoxes working correctly though, so I suspect the problem has to do with the type of user control that we are displaying the listitem as, since the one we got working is using a different user control than the other ones.  I will post the solution if we figure it out.  As always, any other suggestions are appreciated. Thanks.

    - Dan - "Can't never could do anything"
    Friday, February 11, 2011 7:28 PM
  • Hi Deadlydog,

    How is your problem?

    While in RecyclingMode, the ListBox will re-use the same item containers (ListBoxItems) when you scroll down. So I suspect your usercontrol might somehow rely on the container which causes the problem.


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 16, 2011 9:46 AM
    Moderator
  • I haven't had time to test this out yet, but I suspect this is the problem.  Once I have more time to test I'll let you know.  Thanks!
    - Dan - "Can't never could do anything"
    Tuesday, March 08, 2011 2:37 PM
  • This seems to be old problem now for you, but this is exactly what I am solving now. Repeated items on virtualized listBox. Can you give any hints how you solved it? @deadlydog
    Monday, June 06, 2011 5:49 AM