Simple WPF data binding
Written by Mike James   
Monday, 12 July 2010
Article Index
Simple WPF data binding
Two-way binding


Two-way binding

Given that there is a SourceUpdate method you should be able to work out how the system can implement a two way update.

In this case the usual update from source to target works in the usual way - the source class has to implement the INotifiyPropertyChange interface and raise the event when the property changes.




The update from the target to the source is even easier in that the target has access to the BindingExpression object which gives details of the source and provides an UpdateSource method. So all that the target has to do in this case is call UpdateSource when the property changes -easy.

To set a two-way update you simply set the Mode property of the Binding object before using the SetBinding method:

bind.Mode = BindingMode.TwoWay; 

Following this any updates to the source are propagated to the target.

If you set

bind.Mode = BindingMode.OneWayToSource;

Then the source no longer has to implement the INotifyProperty changed interface and the update from target to source occurs anyway.

The only question is what constitutes a change in the target property?

This might seem like a silly question and not one we asked with respect to the source object.

The reason it is relevant now is that the target object is usually a user interface component and in this case the value can change "slowly". Consider a Textbox - does the property change occur when the user types a single new character or when the user has completely finished typing signaled by the Texbox loosing the focus? Obivously it can depend on the situation and the nature of the target.

To cope with this you can set the UpdateSourceTrigger property to one of the allowed values: default, PropertyChanged, LostFocus or Explicit. The Textbox defaults to LostFocus most other controls default to PropertyChanged. Also notice that Explicit means that you have to call the UpdateSource method explicitly.

For example, following:

bind.UpdateSourceTrigger = 

every character entered into the Textbox updates the source property.

Finally there is also the OneTime mode which returns us to the state we started out with a one-time update of the source to the target when the binding is first established. The difference between this and the primitive form of binding we first implemented is that this one-time update occurs even if the source has implementd the INotifyPropertyChanged Interface.

Where next?

This is just the simplest binding between a dependency property and a CLR property that you can create. In practice classes provide alternative ways of creating a binding - but the methods described above always work.

Bindings also implement conversion and validation facilities and we will look at these in a future article. There are also multi-bindings where a single target is bound to multiple sources and in this case conversion methods are important.

Where bindings become really interesting however is when the source is a collection. In this case the update performed depends on the path specified and the nature of the target. More on this too in another article.

Of course we also need to look at using some of the binding conveniences such as additional properties such as DataContext and the use of XAML to describe bindings - again all in another article.

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook or you can subscribe to our weekly newsletter.

Each provides a full list of what's new each week - usually five hot book reviews, five thought provoking articles and five not to be missed news items - in a compact click-to-read form.



Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]

WPF .NET Core - Inside Dependency Properties

One of the great mysteries of WPF is the strangely named "Dependency Properties". In this chapter we learn how dependency properties really work by creating a custom dependency property

WPF .NET Core - Creating Objects With XAML

If you've never encountered WPF (Windows Presentation Foundation) you are missing a versatile tool. This article is part of a series devoted to it. XAML can be confusing - especially if you think it i [ ... ]

Using the WPF .NET 4.0 DataGrid

WPF DataGrid (.NET 4)  can be difficult to understand if you aren't used to thinking about objects and collections. This easy to follow introduction explains where the rows and columns have gone. [ ... ]

Drawing Bitmaps – DrawingImage and DrawingVisual


WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.

Other Articles







Last Updated ( Monday, 12 July 2010 )