Simple WPF data binding
Simple WPF data binding
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.



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.

Custom Shape

Creating a custom Shape isn't difficult but it does have some hidden gotchas. We take a look at how best to do the job.

BitmapSource: WPF Bitmaps

Although bitmap graphics are not a main focus of WPF it does offer some useful facilities

Bitmap Coding and Metatdata in WPF

Having looked at working with raw pixel data we turn our attention to formattted image files and how to code and decode both the pixel data and the meta data they contain.


Routed Events

Routed events were new in .NET 3.5 but there is still plenty to discover about using them. We look at bubbling and tunneling and how to create your own routed even and how WPF changes the underlying m [ ... ]

Other Articles







Last Updated ( Monday, 12 July 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.