Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" causes same list items to always appear

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

like image 472
deadlydog Avatar asked Feb 04 '11 03:02

deadlydog


1 Answers

Recycling Mode works, as it name implies, by reusing rendered ListBoxItems. When a ListBox item is no longer needed to display an item that has scrolled off the list, the ListBox will reuse it to display one that has scrolled into view.

To change what the ListBoxItem is displaying, the ListBox simply sets its DataContext property to the new item. This requires that the contents of the ListBoxItem have to respond correctly to DataContextChanged events. If you are using only using databinding to populate the controls, this will happen automatically. But if, as your comment implies, you are using some code-behind to update the properties, then you need to make sure that your event handling code is doing the job.

like image 110
Samuel Jack Avatar answered Sep 28 '22 15:09

Samuel Jack