Social Icons

Wednesday, 14 August 2013

UpdateSourceTrigger Property in WPF Binding Example


          In this article i will explain
  • What is the purpose of UpdateSourceTrigger Property,
  •  How to use UpdateSourceTrigger Property in WPF Binding, 
  • What are the different Options available for UpdateSourceTrigger Property etc.
  Download Source Code And Video Explanation of UpdateSourceTriggerUsage


             UpdateSourceTrigger is the one of the Property of Binding element in WPF. This Property defines the timing of binding source updates. There are two binding modes which updates the source i.e OneWayToSource and TwoWay.
UpdateSourceTrigger Property Required Modes

        So in this Two Modes we need specify when should occur the binding source updation. 

UpdateSourceTrigger Property has three properties.
  • LostFocus
  • PropertyChanged
  • Explicit. 
To understand this with example, add new window to wpf project and add new window which contains  a ListBox with List of colors and textbox whose Text, Background properties are bind to ListBox.SelectedItem.Content Property.

XAML Code is:

LostFocus:  If set, Binding Source will be updated when target control lost foucs. Now run the above example type one color in textbox and press tab you will notice that listbox is changed and Textbox background is changed.

PropertyChanged: If UpdateSourceTrigger Property is set to PropertyChanged, then source is updated with target data whenever change occurs in Target. Now Change UpdateSourceTrigger Property in our example to PropertyChanged. Now run the example and type in textbox, then ListBox is updated with this content.

Explicit:  If UpdateSourceTrigger Property is set to Explicit, then Binding source is updated only when BindingExpression.UpdateSource( ) method is called externally. Now in our example set the UpdateSourceTrigger Property to Explicitly and add a button to window. Now in button click write below code.

Now run our example and type one color in textbox and then click the button. Now the ListBox is updated with this data instead updating while typing or lost focus.

Default: Instead of these three options, Default option exist which will take place when we didn't specify any option in UpdateSourceTrigger Property. This Default option takes any of three options based on the Dependency Property of Target Control. For example Text Property of TextBox control has default option is LostFocus.

For More Graphical explanation Watch the Video Explaining the UpdateSourceTrigger Property


       I hope you are clear about How to use UpdateSourceTrigger Property in wpf bindings.

           Don't Forget to Comment and Share for our friends if you like this post. Thank You.

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.