0

I have a ListBox bound to a large collection of a custom objects that, among other things, hold a Bitmap.

Currently I'm having problems with the memory because all the bitmaps are loaded even if they aren't visible.

I would like the ListBox to notify my model in some way so I could load/unload the bitmaps on demand as they are needed, in order to avoid having all of them loaded. Thus, only but only load the bitmaps that are visible within the bounds of the ListBox.

How could I do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 1
    Virtualizing should help with speed as well as recycling. You can as well add only few items to your collection and handle scroll change, when the scroller hits the buttom then load add another few to your list and so on. – adminSoftDK May 23 '16 at 16:11
  • Are they shared images? Are the loaded even if visibility is set to collapsed? – Natxo May 23 '16 at 16:43
  • Virtualization won't avoid having in memory the whole collection bound to the listbox! – SuperJMN May 23 '16 at 16:55
  • @Natxo every bitmap is different, generated in run time. There could be around 5000 items in the ListBox. – SuperJMN May 23 '16 at 16:57

1 Answers1

0

If you check ItemContainerGenerator.ContainerFromItem for every item in your collection, it should return null if item is out of view and Virtualizing is enabled.

Or

From Get ListView Visible items:

  • Get the ScrollViewer of the ListView (with a FindDescendant method, that you can do yourself with the VisualTreeHelper ).
  • Read its ScrollViewer.VerticalOffset : it is the number of the first item shown
  • Read its ScrollViewer.ViewportHeight : it is the count of items shown.

Rq : CanContentScroll must be true.

Community
  • 1
  • 1
Natxo
  • 2,917
  • 1
  • 24
  • 36