5

Why is it better (in WPF, C#, Entity Framework) to bind ListBox to an ObservableCollection created upon the ObjectSet (from Entity Framework) rather than binding to ObjectSet directly?

One more question: When I bind ListBox to ObservableCollection, any additions to the collection updates ListBox. Great. But ObservableCollection was created upon ObjectContext (in Entity Framework) and adding a new item to the collection doesn't add the item to the context... how to solve this????

Rachael
  • 1,965
  • 4
  • 29
  • 55
Cartesius00
  • 23,584
  • 43
  • 124
  • 195

2 Answers2

15

(Note to your "One more question" point)

Entity Framework 4.1 offers a new feature which is especially useful in WPF applications - the local view of the object context. It is available through the Local property of DbSet<T>. Local returns an ObservableCollection<T> containing all entities of type T which are currently attached to the context (and not in state Deleted).

Local is useful because it stays automatically in sync with the object context. For example: You can run a query to load objects into the context ...

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

... and then expose the objects in the context as an ObservableCollection ...

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

... and use this as the ItemsSource of some WPF ItemsControl. When you add or remove objects to/from this collection ...

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

... they get automatically added/removed to/from the EF context. Calling SaveChanges would insert/delete the objects into/from the database.

Likewise adding or removing objects to/from the context ...

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

... updates automatically the Local collection and fires therefore the notifications for the WPF binding engine to update the UI.

Here is an overview about Local in EF 4.1.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • How would you handle validation if you did things this way? Would you have the entity models implement IDataErrorInfo/INotifyDataError? – Cocowalla Aug 02 '13 at 13:23
  • @Cocowalla: I believe I did it this way, yes. But I'm not working with WPF anymore since some time, so I can't really tell what's the best way. You better ask a separate question if you'd like professional answers :) – Slauma Aug 02 '13 at 14:41
  • I already asked a seperate question, but thanks for your reply :) – Cocowalla Aug 02 '13 at 14:53
6

ObservableCollection implements INotifyPropertyChanged as well as INotifyCollectionChanged, both of which WPF uses to rebind elements to the UI. Thus, you could add an item to the ObservableCollection and immediately the UI would update with no code interaction from you. ObjectSet implements neither, and so doesnt get this functionality.

Tejs
  • 40,736
  • 10
  • 68
  • 86
  • Terrific! And one other question, if I add a new item to the ObservableCollection then UI know it, but ObjectSet (context) doesn't know any changes. ...how to solve this problem??? – Cartesius00 May 31 '11 at 21:48
  • Whenever you are adding the item to the ObservableCollection, also simply add that to your context. Ex: `MyComlexObject x = new MyComplexObject(); myObservableCollection.Add(x); myContext.ComplexObjects.Add(x);` – Tejs May 31 '11 at 21:51