Share via


Binding.StringFormat only works on TargetType string

I am a fan of BindingBase.StringFormat (introduced in WPF 3.51sp1).
To learn almost every thing you need to know about StringFormat, check Mike Hillberg’s post on StringFormat; he covers MultiBindings, ContentStringFormat, HeaderStringFormat, etc.. 

This week is I wanted to do StringFormat on a CommandParameter  (type object).  I assumed Binding.StringFormat would do the trick but then I tried it (about 5 different ways) and it did not work.    

Per Sam (data binding dev lead) this is a known limitation on 3.5 SP1: if you want to use StringFormat, the target type for your binding needs to be a string.     The workaround for other target types is to to write your own IValueConverter; this is very trivial and Kent Boogaart already has a nice generic Format Value Converter on his WPF converters codeplex project.

[Apologies if post came out uninteresting; it took me a good 45 minutes to digest that StringFormat would be smart enough to care about TargetType, so I wanted to post it to save the next 5 people a little time]..