Using Binding.StringFormat - Rudi Grobler

Using Binding.StringFormat

I know I am a little late on blogging about .NET 3.5 SP1, but here goes... This is a huge release... so much more than just a service pack! The feature that is getting a lot of attention on the net is the new hardware accelerated effect... thy are awesome!

What I want to explorer a little thou is the new Binding.StringFormat!

Value converters are great to shape data being binded too... but if I have to create value converter every time I need to format my text, well... Microsoft has listened too their users and added a very useful StringFormat to the Binding object.

Lets assume I have a data object I am binding too that has a price property... and I need to format the returned value a little. The new syntax makes this very easy

<TextBlock Text="{Binding Price, StringFormat='Price: R{0:000.00}'}" />

Cool!!!

The only thing to remember is that your target property must be of type string (Like the Text property of a TextBlock)!

NOTE: I first tried using the Content property of the Label... not so successful! (Thanks Corrado Cavalli)

UPDATE: n4cer pointed out the ContentStringFormat property!

If you found this useful, please kick it on DotNetKicks.com

UPDATE: Corrado pointed me to this excellent article about Binding.StringFormat by Vincent Sibal

UPDATE: Lester Lebo has a good article about this

Who else blogged about .NET 3.5 SP1?

Scott Guthrie - Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta

Tim Sneath - Introducing the Third Major Release of Windows Presentation Foundation

Scott Hanselman - VS2008 and .Net 3.5 SP1 Beta - Should You Fear This Release

Lester Lebo - .NET 3.5 SP1 - Try it out today

Josef Goldberg - What’s New for Performance in WPF in .Net 3.5 SP1 & Performance improvements in WPF in .Net 3.5 / 3.0 SP1

PS. After I posted this, I saw that Sacha posted about it here

Published Friday, May 16, 2008 7:43 PM by rudi

Comments

# Using Binding.StringFormat

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Friday, May 16, 2008 7:44 PM by DotNetKicks.com

# re: Using Binding.StringFormat

For Content model items like Button, you can use ContentStringFormat rather than StringFormat:

<Button Width="150" Height="50" Content="{Binding Price}" ContentStringFormat="'Price: R{0:000.00}'"/>

Some format strings don't seem to work in the beta, such as {0:X} (hexadecimal).

Saturday, May 17, 2008 5:59 PM by n4cer

# re: Using Binding.StringFormat

Tnx n4cer,

Nice find!

Saturday, May 17, 2008 6:52 PM by rudi

# re: Using Binding.StringFormat

Thanks for the reference Rudi!

Sunday, May 18, 2008 4:12 PM by Vincent Sibal

# re: Using Binding.StringFormat

Nice article Vincent... Keep it up!

I also enjoyed the article about ItemsControl Recycling!!!

Sunday, May 18, 2008 6:24 PM by rudi

# Arjan`s World &raquo; Blog Archive &raquo; LINKBLOG for May 19, 2008

Pingback from  Arjan`s World  &raquo; Blog Archive   &raquo; LINKBLOG for May 19, 2008

# re: Using Binding.StringFormat

Interesting... my problem though is that I don't want to get an incompatible runtime with my server, so I haven't installed .net 3.5. I will just read cool posts like this for now

Monday, May 19, 2008 11:01 PM by Sam

# re: Using Binding.StringFormat

Hi Sam,

I had the same problem, I had 2 systems that I had to commision last week and I had to wait till thy were finished before I upgrade! That is why my article was a little late!

Tuesday, May 20, 2008 5:59 AM by rudi

# Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - May 20, 2008 | Alvin Ashcraft's Morning Dew