Tuesday, July 12, 2011

Binding Modes in WPF


In WPF data binding, four different types of binding modes are available.

OneWay – When source property changed it will automatically update target property. If you bind CustomerName property to TextBlock or Label it will default establish oneway binding and whenever CustomerName property changes it will update Label’s content property. In this Binding mode target property updated only when source property changed.



TwoWay – When source property changed it will update target property and when target property changed will update source property. If you bind customer name property with TextBox’s Text property using twoway binding mode then whenever user changes text in textbox will update CustomerName property and similarly whenever CustomerName property changes will update Textbox’s text property.



OneTime – This binding mode establish binding only one time when your application starts or when you set DataContext property. This type of mode can be used when you want to display data for first time only.


OneWayToSourceUpdates source property on change of target property. This is reverse of OneWay binding. This type of mode is mainly used when your target property is not dependency property.



Default – If you don’t specify any binding mode then default binding will take place. The default binding mode will choose binding mode depending upon target property. For example, if you binding property with Label’s Content property then default binding will be OneWay similarly if you are binding to TextBox’s Text property then default binding will be TwoWay. If you are not sure sometimes it is best to specify binding mode explicitly.  


Related links -
Data Binding in WPF
Dependency Property
WPF Architecture

2 comments:

  1. Thank you. Gone through many site. did not find great explanation like this

    ReplyDelete