Data Binding in WPF

Data Biding in WPF is a way how the data is communicating between the source and target. It is simply updating the data in view(application UI) with respect to data in the view model(presentation logic) and vice versa.

In WPF, Elements (more specifically their properties) can be bound to a variety of data sources, they can be either objects of CLR or XML. See below.

The property of a control can be bound to :

  • a property of another control
  • an object
  • a collection
  • XML data

Binding can be done in XAML or Code(C#.NET or VB.NET).

Binding in WPF always follows a structure / model no matters what element you are binding. There should be a target object, target property, binding source and path to the value of binding source.

For Example :

Suppose you have a textbox in UI which is bound to the ‘Name’ property of a class.
Here we have –Target object – Textbox
Target Property – Text
Source object – your class
Value to use – ‘Name’ Property of class




Some Properties of Binding Class, that are set during Binding

  • Source : Gets or Sets the object to use a Binding Source.
  • ElementName : Gets or Sets the name of the element to use as the binding source object
  • Mode : Gets or sets a value that indicates the direction of the data flow in the binding. (Learn more..)
  • Path : Gets or sets the path to the binding source property.
  • UpdateSourceTrigger : Gets or sets a value that determines the timing of binding source updates. (Learn more…)
  • RelativeSource : Gets or sets the binding source by specifying its location relative to the position of the binding target.
  • Converter : Gets or sets the converter to use. (Learn more about Converters)
  • ConverterParameter : Gets or sets the parameter to pass to the Converter.
  • StringFormat : Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.
  • ValidatesOnDataErrors : Gets or sets a value that indicates whether to include the DataErrorValidationRule.

In addition to the above list of properties, there is a long list of properties provided by Binding Class. You can explore whole list here : MSDN Documentation

Direction of Data Flow – Data Binding Modes

In your application how data flows from source to target can be controlled by setting the mode property of Binding object.
WPF have four Modes – OneWay, TwoWay, OneWayToSource, OneTime

Learn about Modes in Detail.

What triggers when source changes or target changes ??

Source Changes

To reflect changes from source to target , you must require to implement INotifyPropertyChanged interface. (Read about INotifyPropertyChanged)

Target Changes

To control data flow from target to source, set ‘UpdateSourceTrigger’ property.

How to create a Binding – DataContext

For setting up binding we need to Specify Binding Source object. DataContext is one way of doing it. It is actually a dependency property. It gets or sets the data context for an element which is involved in binding. It allows all the child elements to inherit information about the data source from parent element. (Learn more about DataContext)

Syntax :

Data Conversion During Binding

IvalueConvert

Suppose you are binding two objects that are taking different property types. Let us say you have a Button which is taking value of Color type for its Background Property and is bound to a property of a class ‘MyClass’ which is of String type. So how a string type value is converted to a Color type value. For this reason Data Conversion comes into account.

Learn more about ValueConverter and MultiValueConverter

Hope it helps !!!