Page 3 of 3
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:
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.
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.
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. [ ... ]
FlexGrid - A Lightweight Data Grid
There are more data grids available than the standard one that comes with WPF. In this article we take a look at FlexGrid for WPF and discover how easy it is to use.