What are the various WPF binding modes?

WpfData Binding

Wpf Problem Overview


I do not understand the various data binding modes in WPF, such as:

  • One-Way
  • Two-Way
  • One-Time
  • etc...

What does each of these modes mean?

When should they be used?

Wpf Solutions


Solution 1 - Wpf

  • OneWay: Use this when you want the bound property to update the user interface.
  • TwoWay: This has the same behavior as OneWay and OneWayToSource combined. The bound property will update the user interface, and changes in the user interface will update the bound property (You would use this with a TextBox or a Checkbox, for example.)
  • OneTime: This has the same behavior as OneWay, except it will only update the user interface one time. This should be your default choice for binding (for various reasons I won't elaborate on here). You should only use other types of bindings if you actually need the extra functionality.
  • OneWayToSource: This is the opposite of OneWay -- user interface value changes update the bound property.

If you don't specify anything, then the behavior will depend on the control that you are using.

For more info, see BindingMode enum on Microsoft Docs.

Solution 2 - Wpf

A binding consists of two entities:

  1. The Source (Typically the ViewModel in MVVM scenarios)
  2. The Target (The UI control)

The target has to be a DependencyObject (for binding to work) and the source can be either a DependencyObject or it should have some mechanism to imitate the WPF Binding system about it being changed (Implemeting INotifyPropetyChnaged interface).


MVVM recommends the ViewModel project to be free from any View related references and hence it is recommended to use INotifyPropertyChanged interface to make the Source object being heard by WPF binding. Binding happens between a property of Source and a property of Target (has to be a DependencyProperty). e.g. The TextPropertyof the TextBox class is DataBound to (say) UserName property of the view model. WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise.

  1. OneWay: The target property will listen to the source property being changed and will update itself. If you programmatically change the ViewwModel's UserName property, it will reflect in the text box. This is of intermediate cost as the binding system watches only Source for changes.
  2. TwoWay: The target property will listen to the source property being changed and will update itself. AND The source property will listen to the target property being changed and will update itself. Both the TextProperty and the UserName property will remain in sync and will update each other if one changes. This is most costly as the binding system has to watch both sides for change.
  3. OneWayToSource: The Source property will change if the target property is changed. If the user changes the TextProperty, the UserName property will take up the changed value. This again is of intermediate cost as the binding system watches only Target for changes.
  4. OneTime: This happens only once during the lifetime of Binding, the Target property will be updated with the Source property when the Binding happens. This is least costly and is advisable for scenarios where you have static data to be shown e.g. Label, TextBlock etc.

If you don't mention anything, every target property has a default binding mode associated with itself. E.g. the TextProperty of a TextBox has default binding mode as TwoWay. For the TextProperty of a TextBlock it is one way.

It is advisable that you choose the right mode as it can help you reduce the application latency especially in cases where you have large number of controls in your UI.

For more on MVVM here is an article written by me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDATT OZAView Question on Stackoverflow
Solution 1 - WpfDaveView Answer on Stackoverflow
Solution 2 - WpfJamesView Answer on Stackoverflow