ObservableCollection is better than ObjectSet

Why is it better (in WPF, C #, Entity Framework) to bind ListBoxto ObservableCollectioncreated on ObjectSet(from Entity Framework), and not to bind to ObjectSetdirectly?

One more question: When I bind ListBoxto ObservableCollection, any additions to the collection are updated ListBox. Fine. But it ObservableCollectionwas created on ObjectContext(in the Entity Framework), and adding a new item to the collection does not add the item to the context ... how to solve this problem?

+5
source share
2 answers

ObservableCollection INotifyPropertyChanged, INotifyCollectionChanged, WPF . , ObservableCollection . ObjectSet , .

+6

( " " )

Entity Framework 4.1 , WPF - . Local DbSet<T>. Local ObservableCollection<T>, T, ( Deleted).

Local , . : ...

dbContext.Customers.Where(c => c.Country == "Alice Wonderland").Load();

... ObservableCollection...

ObservableCollection<Customer> items = dbContext.Customers.Local;

... ItemsSource WPF ItemsControl. / ...

items.Add(newCustomer);
items.Remove(oldCustomer);

... / / EF. SaveChanges / / .

/ ...

dbContext.Customers.Add(newCustomer);
dbContext.Customers.Remove(oldCustomer);

... Local WPF .

Local EF 4.1.

+15

All Articles