Home > Data Binding, WPF > UpdateSourceTrigger – DataBinding

UpdateSourceTrigger – DataBinding

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.

About these ads
  1. Tushar
    February 24, 2010 at 4:11 am | #1

    Thank you. You simplified your explaination to my understanding.

  2. June 15, 2010 at 5:33 pm | #2

    thank you, nice share,,

  3. June 17, 2010 at 2:30 am | #3

    Excelent post. I had EXACTLY the same problem with textboxes and I was not able to figure out what was happening. Thanks a lot!

  4. kimic
    November 6, 2010 at 3:58 pm | #4

    i have run into very same problem, i have been looking for this bug about 8 hours, during that time i figured that problem started when i upgraded from 3.5 .net to 4.0 two weeks ago, just noticed it now. Using datagrid and datatemplate for rowdetails started to ignore changes between row and template. When i was on the half way of rewriting components to use binding on active element, i have found this simple solution on your page:
    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    which solved everything. thank you

  5. Neil B
    June 18, 2012 at 10:19 pm | #5

    Thanks, I had an idea this was my problem and that cleared it up nicely. Good pos

  6. Gor
    October 14, 2012 at 8:43 pm | #6

    Thank you so much

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: