Skip to main content

Posts

Showing posts with the label StringFormat

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