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

BindingExpression and a one-way binding

What actually happens when you do a SetBinding method call is that a BindingExpression object is created and stored as part of the target object. It is the BindingExpression that "knows" enough about the source and target to actually do the update.

 

Banner

 

You can retrieve the BindingExpression object at any time using the target objects GetBindingExpression method.

For example, to get the BindingExpression object associated with the Textbox and the TextProperty dependency property you would use:

BindingExpression bexp= 
textBox1.GetBindingExpression(
TextBox.TextProperty);

The BindingExpression object has two key methods - UpdateTarget() and UpdateSource() - and you can guess what they do. These are the methods that do the actual work of the binding - they update the properties.

For example, to make the source update the target when its property value changes all we have to do is add a call to UpdateTarget:

SourceObject.datavalue = 50;
bexp.UpdateTarget();

or equivalently

textBox1.GetBindingExpression(
TextBox.TextProperty).
UpdateSource();

Of course this is a crazy way to implement one-way binding because you need to know which BindingExpression object to use to update the source. As do things this way you might as well simply update the source directly.

INotifyPropertyChange

The point is that a binding update works by calling either UpdateTarget or UpdateSource and the only problem we have left is to find a way to do the job automatically.

The standard solution is to use the INotifyPropertyChanged Interface - but notice that you can implement alternative ways of doing the same job.

This said most databinding makes use of INotifyPropertyChange.

The INotifyPropertyChange interface has one member the PropertyChanged event and this is what the source class has to implement and raise whenever a property changes:

class SourceClass:INotifyPropertyChanged
{
public event PropertyChangedEventHandler
PropertyChanged;
private int MydataValue;
public int datavalue
 {
  get
  { return MydataValue; }
  set
  {
   MydataValue = value;
   if (PropertyChanged != null)
   {
    PropertyChanged(this,
new PropertyChangedEventArgs(
"datavalue"));
   }
  }
}
}

You can see that our new version of the source class implements the INotifyPropertyChange interface by declaring the PropertyChanged event. It also raises the event when the datavalue property is set i.e. changes. Also notice that it passes the name of the property that changes in the event arguments. You can raise the same event for any property changes - simply supply the name of the property that has changed. If you leave the name null then it is assumed that all of the objects properties have changed.

Now if you try the program out you will discover that changing the value of the datavalue property always updates the Textbox target property.

We now have a one-way automatic update implemented as it is usually implemented. What actually happens is that when you use the SetBinding method the UpdateSource method is registered as the event handler. This registration isn't direct as there are a number of other methods and objects in between the event and the handler but this is conceptually what happens.

Banner

<ASIN:1430272406>

<ASIN:0470502223>

<ASIN:1430272406>

<ASIN:0672331195>

<ASIN:0470502258>



Last Updated ( Monday, 12 July 2010 )