Archive

Posts Tagged ‘WPF Unleashed’

UpdateSourceTrigger – DataBinding

August 22, 2009 6 comments

Last week while working on two way binding, I discovered one very important settings for binding i.e. “UpdateSourceTrigger”. Before discovering this feature I faced problem in setting the value from target to source. Let me describe the problem I faced.

In my setup, I had no. of text boxes in window whose text property were bound to the property of object in collection. So in this case, source for me was property of the object in collection and target was the text property of textboxes.

I configured my binding object as below :

            Binding binding = new Binding();
            binding.Source = <source object from collection>;
            binding.Path = new PropertyPath
(<PropertyName>);
            binding.Mode = BindingMode.TwoWay;
            txtbox.SetBinding(TextBox.TextProperty, binding);

With above binding configuration, source –> target binding worked perfectly for me. But binding from target –> source (i.e. from text box to object’s property in collection) was not working for me. One thing which is important to mention here is, I was changing the text box’s text value in procedural code and not by entering the value in textbox.

It was beyond my understanding why binding was not working? I spent almost half a day trying to figure out this. I googled it but google disappointed me. So I took Adam Nathan’s “WPF Unleashed” book in my hand and started looking for some solution. Again this book came to my rescue. While going through the “Data Binding” chapter in this book, I learned about “UpdateSourceTrigger’ property of the binding object.

“UpdatesourceTrigger” property is very important in “TwoWayBinding” and “OneWayToSource” binding modes. It defines how and when source should get updated.

Value of UpdateSourceTrigger could be any of the value from UpdateSourceTrigger Enumeration. Enumeration values are

  1. PropertyChanged – The source is updated whenever the target property value
    changes.
  2. LostFocus – The source is updated when target property changes and target object looses focus.
  3. Explicit – The source is updated when explicit call is made to update using “BindingExpression.UpdateSource”.

Target controls have default values set for “UpdateSourceTrigger” for binding object. For text box control, default setting for this property is “LostFocus”. In my case, I was updating the textbox (i.e. target) through code and because of that textbox was not loosing focus. Hence my binding was not working. Then I changed above binding configuration to set the UpdateSourceTrigger property of binding object as following :

            Binding binding = new Binding();
            binding.Source = writeItemInfoList[index];
            binding.Path = new PropertyPath("ItemValue");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            txtbox.SetBinding(TextBox.TextProperty, binding);

And Binding from Target to source started working for me.

Advertisements