views:

27

answers:

4

Hello,

As i was reading about ObservableCollection i came to know that it implements INotifyPropertyChanged which means whenever the view changes it's underlying collection also gets updated automatically which is exactly what is the purpose of TwoWay databinding. So, what is the difference between ObservableCollection and TwoWay databinding?

Thanks in advance :)

+1  A: 

The question is a little bit like "what's the difference between a horse and a cart?"

An ObservableCollection can be used for TwoWay databinding (to an ItemsControl).
It is more or less designed to make databinding easy.

Whenever a Control updates an ObservableCollection or the other way around, there is a DataBinding in the middle.

Henk Holterman
+1  A: 

Hi!

If you bind to your collection like this {Binding Path=MyCollection, Mode=TwoWay} then reference to collection will be updated but not its contents. Observable collection allows to track its contents changes, but not reference change (MyCollection.Clear() will trigger UI update, but MyCollection = anotherInstance will not).

levanovd
@levanovd :- And what is the `MyCollection` is instance of ObservableCollection? Then will it update the reference or the contents?
Ankit Rathod
If you call OnNotyfyPropertyChanged("MyCollection") when you change reference to your collection then WPF will track both reference and contents changes.
levanovd
+1  A: 

Object needs to implement INotifyPropertyChanged so you are able to bind to properties of this object.

TwoWayDatabinding tells databinding engine it needs to update properties both ways.

You are comparing apples and airplanes. Those are two completly different things. Also ObservableCollection also implements INotifyCollectionChanged that allows to notify its items changed, not only properties.

Euphoric
+3  A: 

The two "things" TwoWay databinding and ObservableCollection are different things which can collaborate, but are not directly linked. Databinding is a concept and ObservableCollection is an implementation of an interface (INotifyCollectionChanged) which is used in the implementation of the concept of databinding in the .NET framework. In itself, INotifyCollectionChanged is a small part of this implementation.

In fact you can use TwoWay databinding without ObservableCollections and ObservableCollections without databinding.

Databinding is a mecanism allowing you to bind a business object property (Often a ViewModel property) to an UI property. OneWay Databinding provides support for update of UI when the object is changed (which requires implementation of INotifyPropertyChanged and/or INotifyCollectionChanged on the business object). TwoWay Databinding provides twoway support as its name suggest it: Object => UI (like OneWay do) AND UI => Object. UI to Object updates does not requires implementation of INotifyCollectionChanged nor INotifyPropertyChanged.

Observable collection now is interesting because it implements INotifyCollectionChanged, which makes it a good candidate to create databindable collections. But I use often INotifyCollectionChanged without databinding, and I am pretty sure you can find other collections which implement INotifyCollectionChanged (like PagedCollectionView)

Does it answer your question?

Maupertuis