Skip to main content


Showing posts with the label UpdateSourceTrigger

Gotcha with StringFormat

Hope most of you have used StringFormat property on your binding to render the formatted value on the user interface. But are you aware about one of its secret. Well before revealing that secret, let’s have a look at how StringFormat works with binding. Scenario problem: I am taking an example scenario, in which my text box will display amount till three decimals.  Now there are multiple ways to achieve this. One way can be by using Converters, another way can be by using StringFormat along with TextBox binding. Perhaps there can be more ways apart from these two ;) In below sample I am going to take StringFormat trick to achieve this and code to perform this operation is very straight forward as: <TextBox Text="{Binding Amount,StringFormat=f3}" /> With above code, whenever you will lost focus from your TextBox, given amount will be displayed as three decimal points. Till here everything is perfect as expected BUT with one downsize. Important

UpdateSourceTrigger in WPF

This is a property on a binding that controls the data flow from target to the source and used in two-way databinding scenarios. The default mode is when focus changes but there are number of other options available, which we will see in this article. Data trigger scenarios: Let’s talk about some of the data trigger scenarios.  By default, modified values in the binding controls only get pushed down when we do activity related to focus change like tab out, minimizing and maximizing window, etc. ·     In some scenarios we want to update values as quickly as possible, or let’s say with every key stoke.  So, to achieve such kind of scenarios Microsoft provided  UpdateSourceTrigger .  Properties available with  UpdateSourceTrigger : Default – This is the default value and it means a lost focus for most of the controls LostFocus – Value updation will be on hold until the focus moves out of control PropertyChanged – Value updation will happen whenever a target property