Reputation: 554
Following on from this previous question:
WPF needs ObservableCollection
to bind the data. I know I can create the ObservableCollection
by using a list in the constructor like:
new ObservableCollection(myListName)
But will the nested groups bind to the WPF form or do I need to run the result of my linq query into a pre-defined type with ObservableCollections
at each level?
Upvotes: 1
Views: 315
Reputation: 177163
You can use directly ObservableCollection<School>
and ObservableCollection<Class>
instead of IList<School>
and IList<Class>
in your model classes. This way you ensure that Entity Framework will materialize the collection as ObservableCollection<T>
when you use eager or lazy loading. With IList<T>
EF would create a List<T>
as the concrete type and not an ObservableCollection<T>
.
If your navigation properties are marked as virtual
you are using lazy loading which means that EF will load the collections automatically as soon as your WPF form wants to access the properties. (The context must not be disposed to get this working.)
If you want to load all collections at once in one query you can use eager loading:
var students = new ObservableCollection<Student>(
context.Students.Include(s => s.Schools.Select(sc => sc.Classes)));
This blog post might be helpful as well.
Upvotes: 1