Silverlight 4 Binding and StringFormat in XAML
I discovered this amazing feature almost by accident and it has made this one part of my design so much easier that I had to share it.
A new feature in Silverlight 4 is the ability to using the StringFormat feature when binding. Previously, if I wanted to have a piece of text that said “Your name is [username]” I could either use the old Horizontal-Stack-Panel-And-2-TextBlocks trick (as seen below)…
<StackPanel Orientation=”Horizontal”>
<TextBlock Text=”Your name is” Margin=”0,0,4,0″/>
<TextBlock Text=”{Binding username}“/>
</StackPanel>
…or write a value converter (not going to be seen below because there’s a great example of it over here. Incidentally, that example is totally irrelevant if you’re going to use StringFormat, but more on that in a second).
The StringFormat option in Silverlight 4 allows you put all that information into a single field, which is extremely useful not only for TextBlocks, but for Content fields in a Button. In fact, let’s use that as an example.
Let’s say you want to create a Button to log out, so you want it to say “Log Out of <Username> Account”. (A bit clumsy, but the technique is the important part.) All you would have to do is the following:
<Button Content=”{Binding username, StringFormat=’Log Out of \{0\} Account’}“/>
This gets even better for things like number formatting. Let’s say we want the user to enter an amount of money (for example, $1,593.29) into a TextBox (maybe in a PayPal application). If we have bound that value to a numeric format, we can express that format through binding and when the TextBox loses focus, the StringFormat will take the number entered and format in a currency format.
<TextBox Text=”{Binding paymentAmount, StringFormat=\{0:C2\}}“/>
The only issue with numerical and date formats is they the MUST be bound to a number or date.
With that in mind, here is a sampling of StringFormat options, stolen mostly from Kathy Kam. For more complete options, check out the MSDN articles on String.Format and trial-and-error your way through things. If you want to play around with this, download my StringFormat project or look at the Silverlight sample app at the bottom of this page.
Strings
For a string with the value “Silverlight”
Using \{0\,#\} effectively forces the string to be at least # characters long, using spaces to pad it to the requested length.
StringFormat=\{0\,20\} : “ Silverlight”
StringFormat=\{0\,-20\} : “Silverlight ”
StringFormat=’I just typed "\{0\}".’ : “I just typed “Silverlight”.”
MSDN article on Composite Formatting
Numbers
For a double with the value : “38293.53”
StringFormat=c : “$38,293.53” – Use ‘c’ for currency
StringFormat=e : “3.829353e+004” – Use ‘e’ for exponential (scientific)
StringFormat=n : “38,293.53” – Use n for number
You can also use these in the following format:
\{0:(letter)(number)\}
where (number) indicates how many decimal places there should be. The format will use standard rounding rules to determine the last digit. For example:
StringFormat=\{0:c0\} : “$38,294”
StringFormat=\{0:n4\} : “38,293.5300”
StringFormat=You have \{0:c1\} : “You have $38,293.5”
MSDN article for standard number formatting
MSDN article for custom number formatting
Dates
The date formatting has a huge range of options.
For the DateTime of “April 17, 2004, 1:52:45 PM”
You can either use a set of standard formats (standard formats)…
StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
StringFormat=g : “4/17/2004 1:52 PM”
StringFormat=m : “April 17”
StringFormat=y : “April, 2004”
StringFormat=t : “1:52 PM”
StringFormat=u : “2004-04-17 13:52:45Z”
StringFormat=o : “2004-04-17T13:52:45.0000000”
… or you can create your own date formatting using letters (custom formats)
StringFormat=’MM/dd/yy’ : “04/17/04”
StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”
MDSN article for standard date formatting
MSDN article for custom date formatting

Cool tip man !!!!!!!! Thanks for sharing !!!!
Thanks for sharing! I can get rid of tons of DataConverters on my code just by using this.
[...] Silverlight 4 Binding and StringFormat in XAML (Matthias Shapiro) [...]
[...] Silverlight 4 Binding and StringFormat in XAML (Matthias Shapiro) [...]
Very well done article. The Silverlight app at the bottom is very impressive. I’m sure that took some effort. It is appreciated.
Diggin’ this. Totally great way to bind with formatting.
Thanks for the tip. This is a great feature that I had no idea about.
Thank you wery much! Great post!
Another thing you can do is specify different formatting for positive, negative and zero value numbers. For example, if you want to always have a positive or negative sign in front of your value, you would use the following {Binding Path=MyNumber, StringFormat=’+##;-##;0′}
Anyone know how I can show a Currency format with 2 decimal places, ONLY if there are decimals? e.g.
25 should show as $25 and 25.01 should show as $25.01
http://stackoverflow.com/questions/3219116/string-format-expression-to-show-currency-with-2-decimal-places-only-if-there-are
How can I change the currency symbol to £ instead of $.
My PC’s Regional setting are set to UK but still showing $ as currency symbol
[...] Designer Silverlight site has more [...]
You write Good articles,keep up good work.