Search code examples
c#.netwpfautocompleteboxdata-virtualization

WPF AutoCompleteBox Data Virtualization


I am trying to implement Data Virtualization on a WPF AutoCompleteBox. I found Bea Stollnitz's code here which works great on a ListView and I made it to work on an ComboBox easily, but there's no way I can seem to get it working right on an AutoCompleteBox.

To be precise, it works - the list is virtualized, items are not all loaded on startup - but what seems to happen is that the AutoCompleteBox iterates through all the items in the list as soon as the ItemsSource changes and this ends up breaking the whole thing (i.e. on load, all the pages in the list are requested, so even though they are virtualized, they will all be requested in the beginning and get loaded). My guess is that this is because of the filtering that the ACBox does, but I'm surprised that there's not way to prevent it, since the control does normally allow filtering to be done on the server-side using the Populating event.

I feel that I'm missing something, I can't believe that no one has done something like this before or that it can't be done, so I'm guessing that I'm just doing something obviously wrong that I can't figure out since I'm new to WPF.

Here's some of the things that I've done to try and get this together (based on Internet searches around similar problems):

  • I made sure all the conditions for the built-in UI virtualization are ok including explicitly turning it on, setting the max height of the ListBox etc.

  • I replaced the ListBox in the AutoCompleteBox with a ListView like the one that Bea uses in her example. Side by side, the ListView by itself works as expected, but the one embedded in the ACBox does not.

  • I tried using no filtering in the ACBox, using a custom filter, handling the populating evenet manually etc. This doesn't help. Making a custom filter is obviously not enough since the custom filter only allows you to specify the result of evaluating one item, the code that loops through the list is not visible so you can't prevent looping. Turning the filter to "None" doesn't do anything either.

Any and all suggestions are welcome!

The target for this is .NET 3.5 and I am using the WPF Toolkit (Feb 2010 release)

Thank you!


Solution

  • I tracked this down to OnItemsSourceChanged() (see source). In there, the AutocompleteBox stores a "local cached copy of the data", which is why I see the behavior noted above. This is a private method, so no overriding here.

    It seems to me that because of this you can't apply DataVirtualization to the AutoCompleteBox, at least not using the ideas in Bea's solution. If anyone has any different thoughts regarding this, I would love to try it out, but until then, this is what I believe the answer to be.