I'm rather new to WP7 dev and I'm trying to play with data binding. I have an XML file and I want to represent it in a ListBox. I've made a ModelView on which I make a Linq query, the result of which I want to put in a ListBox. I've seen that many examples of DataBinding use the ObservableCollection class. I would like to use Linq to get the results instead of looping row by row, but if I use the IEnumerable instead of ObservableCollection, DataBinding does not work. Is that normal?
4 Answers
Silverlight works on a property changed mechanism to be notified when a property has changed in order to update a data binding. You can either use dependency properties (highly unlikely for a view model) or implement the INotifyPropertyChanged
interface in your view model class.
In the case of ObservableCollection
, it actually raises the CollectionChanged
event when it's contents change (add/remove/insert), which also prompts the data binding to update. In your case, by binding to an IEnumerable
, the data binding mechanism doesn't know when the contents has changed because there are no events raised.
If every action results in a new list, then you're not gaining anything by creating an ObservableCollection
each time you re-query. So you'll need to implement the INotifyPropertyChanged
interface and raise property change notification for yrou IEnumerable
property when you re-query.
If you have items to add/remove each time you re-query, then creating an ObservableCollection
the first time and then calling Add
/Remove
on each query will work just fine.

- 16,179
- 36
- 51
I guess it has to be an IList
to work. IEnumerable
won't work.
Try adding .ToList()
to your LINQ
query to convert it to List<T>
.
As for ObservableCollection<T>
, you need to use it if you plan to change items in that collection so that the UI
automatically refreshes itself.

- 30,394
- 6
- 63
- 80
You can create an ObservableCollection
from an enumerable by passing it to one of the constructors for the OC.
ObservableCollection<Foo> x = new ObservableCollection<Foo>(enumerable);
Courtesy of Cast LINQ result to ObservableCollection
Of course, this only applies if you want to avoid looping through the records yourself and don't have a reason due to behaviour in the code.

- 1
- 1

- 65,560
- 11
- 91
- 143
but if I use the IEnumerable instead of ObservableCollection, DataBinding does not work
Using an IEnumerable
for your list of data items is not the problem, in fact the definition for the ItemsSource
property on the ListBox
is:
public IEnumerable ItemsSource { get; set; }
Most likely the issue is that your property is not notifying when its value has changed (use the INotifyPropertyChanged
interface).
The ObservableCollection
type is a collection that implements INotifyCollectionChanged
, so you can easily tell when the collection itself has changed, although this doesn't notify you when the properties on the collection items themselves have changed. It also implements INotifyPropertyChanged, which is most likely why your data binding was working on your property in your ViewModel. The reason why the ObservableCollection
is good to use in this case is because you can just add and remove items to the collection to update the list based control, you don't have to rebind it (or rebuild a list).
So if you bind to a IEnumerable<>
property, make sure you raise a notification that the property has changed, and be aware that adding/removing individual items will not get reflected automatically in the UI unless you devise a way of notifying that the bound property has changed (which would be a bit of a hack).

- 49,403
- 14
- 95
- 145