4

I have an IEnumerable<> which lazy loads it's data. I want to just set a Combobox's ItemsSource to the IEnumerable, but when I do it goes and loads all the data anyway (which removes the point of lazy loading).

I've tried it with Linq-To-Sql as well since it seems to be a similar theory and it also loads all the data.

Is there an easy way to do this?

1
  • Create an event on your IEnumerable class, say OnDataChange, which will reset the DataSource. If that fits your requirements
    – PostMan
    May 25, 2010 at 1:06

6 Answers 6

3

Try setting the IsAsync-Property in the ItemsSource-Binding of the ComboBox to True:

<ComboBox ItemsSource={Binding YourItemsSourceProperty, IsAsync=True}
          SelectedItem={Binding YourSelectionProperty} />

If that does not change anything, have a look at this one: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3d343489-90c4-4bdc-8bd9-1046ec9daf76 Maybe you will need to use IList instead.

Alternatively, you could use PriorityBinding, to fill the list with some temporary data until the final list is completely loaded.

2

Don't bind the control to the IEnumerable directly. Instead, bind it to a ObservableCollection (which is empty at the beginning.) Meanwhile, still do your lazy loading on the IEnumerable as usual (either triggered by drop down combobox or something else.) While the data is loaded or when you have enough data, add the items to that ObservableCollection to populate the comboBox.

1

I don't think the WPF ComboBox supports lazily loading the items from the ItemsSource. Why do you need to lazy load anyway, and when would you expect it to trigger the lazy load?

1
  • It's for choosing from large lists. I guess it's more of an auto complete than a combobox. I know I could bind to the text, filter and return the first x results in a property bound to the ItemsSource, I was hoping for something a bit more generic. I thought something similar could be used for ListBoxes to give something like Bing image search. May 25, 2010 at 1:38
1

Bind your ComboBox's ItemsSource to an ObservableCollection.

Now whenever your IEnumerable lazy loads the data, add it to the ObservableCollection instantly

foreach(Item i in myIEnumerable)
{
    myObsCol.Add(i);
}

This would update the UI once each item is added.

1

I am trying to do same thing. But as I investigated, if you want to use standard bindings on combobox (collection to ItemsSource and dataItem to SelectedValue/SelectedItem), it is necessary to write your own control. Combobox is inherited from Selector and when you have bounded collection to ItemsSource property and you change your value of property that is bounded to SelectedValue/SelectedItem then the Selector call it's own private method FindItemWithValue(object value). This method walks through items in bounded collection from first until it finds equal value. That of course will make you collection to load all items before the selected one.

1

If you are willing to do your own custom class that will have a list, you can use INotifyPropertyChanged interface to tell that your collection has been modified. Or as use ObservableCollection as it has been already suggested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.