3

I have a listbox that can potentially have a large number of items with backing data. In order to handle data virtualization, I have tied into WPF's UI virtualization by writing a collection that implements the same interfaces as ObservableCollection and can spin up our backing data as needed.

This works fine. The problem comes when I introduce multi-select with the listbox. Because listbox manages selected items by object rather than index (SelectedItems), selecting all of the items causes all of the backing data to be spun up and held in memory by the listbox, defeating the data virtualization...

Is there a way to prevent this from happening other than re-writing ListBox to manage selection by index instead of object?

Thanks!

Eric

Wolf
  • 9,679
  • 7
  • 62
  • 108
esc861
  • 63
  • 4
  • It sounds like you got exactly what you wanted, since you want items to load on demand. In order to "select all" you need to have them loaded, right? TBH, I would override the CTRL-A (select all) of the listbox and tie it to a command that doesn't do anything (basically disable it). Whether you can do this through binding or if you need to derive a subclass of the listbox, I'm not sure. – Bahri Gungor Sep 28 '11 at 14:29
  • Not quite. I only want items that are actually visible to have their data loaded, but I want the listbox to maintain the selected indices list even if an item is off-screen so that it is selected when it becomes visible. I do want to allow users to use ctrl-a too, I don't want to stop them from selecting all items. – esc861 Sep 28 '11 at 14:47
  • Sorry I do not access to the code to post right but I had a similar situation and what I did was move to ListView (but I think you could do it with a ListBox) Single select and use a template with a CheckBox. That way you are handling the check (select) at the item level and not requiring the ListBox to manage a collection of selected items. I did this for better control of handling the Check event but I suspect it also work with virtualization. Use UpdateSourceTrigger=PropertyChanged as with the LostFocus event you may have a problem of the ListItem gone. – paparazzo Sep 28 '11 at 16:25
  • As for the ctrl-a that would be very interesting with virtualiztion. And with the item level checkbox approach I suggested ctrl-a is not going to work. If the class the checkbox binds to implements INotify then you could select all the items in the collection and the UI would update but wiring that up to ctrl-a could be a problem as some controls don't let you overwrite their hot key. – paparazzo Sep 28 '11 at 16:34

2 Answers2

0

Why don't you use the Virtualizing option on the UI control? This will only load the data that is to be displayed on the screen.

http://www.kirupa.com/net/ui_virtualization_pg1.htm

tsells
  • 2,751
  • 1
  • 18
  • 20
  • Is this approach capable of multi-selecting a lot of entries, say 70K? – Wolf Jan 14 '15 at 13:04
  • I'm not even sure why / how a UI would want to select that many entries at one time. However, I don't see why it would not be able to handle it. You would select the entries either manually through the UI (take forever), or via a code call. The virtualization only applies to the displaying of the control on the screen - not the control itself / backing data in memory. – tsells Jan 14 '15 at 18:14
  • Ok, I just tried it with a million items (simply using the index as text for data). Select all ([Home] then [Shift][Ctrl][End]) works pretty fast. BTW: I use the virtualized list box for displaying the contents of a log file. Now, the user should be able to copy portions of it to the clipboard. – Wolf Jan 15 '15 at 11:53
0

We were using UI virtualization to control our data virtualization, but were having problems with listbox holding onto selection, so our data was not being unloaded.

What I ended up doing was tieing into the item generator for the virtualizing stack panel and listening to the data context changed event for the generated containers. When this happened, I either unloaded the real data from our wrapper.

esc861
  • 63
  • 4