Data Binding

Binding allows you to link a source object to some control. There are two types of bindings:

  • One Way Binding: Binds a source to the interface.
  • Two Way Binding: Binds a source to the interface and back to the source.
INotifyPropertyChanged interface allows sources to interact with the interface and update it as the values change.
To bind an object or list to an element you must set the DataContext property.
It is possible to bind an object or a list of objects and you can bind one element to another.
To customize how bound data will be displayed you can set the DataTemplate from a control.
It is possible to set Data Converters to convert the source type to another type.

DataContext

Is the property that defines the data a control holds. A model is often assigned to the Window’s data context on a MVVM pattern.

or

One Way Binding
Binds the values from the code to the screen. But not from the screen back to the code.

A label control is not an input control so it takes OneWayBinding by default.

Two Way Binding
Binds the values from the code to the screen and from the screen back to the code.

A textbox control is an input control so it takes TwoWayBinding by default.

By default the source property will be updated when the TextBox loses focus. This behavior can be changed by setting the UpdateSourceTrigger.

INotifyPropertyChanged
Calls an event called PropertyChanged informing which property had its value changed. Required to inform when the code changes and the interface should update.
In .NET 4.5+ it is possible to use the parameter attribute `[CallerMemberName]`. It means that the parameter is going to receive the name of the caller by default. With that you can just call the method without writing the string name of the property you are changing.

Without .NET 4.5 you have to send the name of the property as `NotifyPropertyChanged(“Title”)`.
And as an workaround to writting hard-coded strings there is the lambda approach where the member name is extracted from an expression as in:

There is another option which is IL injection. There is a Nuget Packaged called Fody and it has a module called Fody PropertyChanged. You can decorate a class with the attribute [PropertyChanged.ImplementPropertyChanged] and in the build process Fody is going to inject code to notify of property changes on the properties of your class.
Element Binding
Binds the values from another control rather than the values from the Data Context.
List Binding
Binds a collection to a control. The collection used in often ObservableCollection<T> as it notifies the interface of any changes.

Data Templates
Can be overwritten to display data in a custom format.
The default way a ListBox renders its items is by calling ToString. However if you are binding a list of objects you may want to customize how results are displayed. By the default you will get the result bellow.

But you can change the data template to show the values you want, for example:

Data Conversion
Can convert a value from one type to another. A common use is when you have to convert a boolean value to set the control’s visibility.
By simply binding a boolean value to the visibility it is not going to work, because the property expects the type “Visibility”.

 So we can create a converter and apply to the bindings.

Which then shows the expected result:

Advertisements

3 thoughts on “Data Binding

  1. Pingback: WPF Controls: Form controls | Coding Wise

  2. Pingback: The MVVM pattern in WPF | Coding Wise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s