views:

6716

answers:

4

I know that WPF 3.5 SP1 supports a StringFormat in a binding, but can Silverlight do the same? I thought it could, but damned if I can make it work!

Here's a snippet of my XAML:

<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding StartTime, StringFormat=t}" />

It compiles OK, but I get a runtime error when it gets to the browser...

+3  A: 

It does not but you can use a Value Converter and specify the ConverterParamenter in the binding. You'll have to create your own Value Converter by deriving a class from IValueConverter like I've shown here. Michael

Michael S. Scherotter
SL4 does. http://msdn.microsoft.com/en-us/library/cc278072(VS.95).aspx
Simon_Weaver
A: 

Thanks Michael.

Yes, I've already gone down the IValueConverter route.

It just gets a little confusing when working in both WPF and Silverlight as to what's supported in each environment! :-)

Craig Shearer
No kidding. Makes you wish that WPF could just be a clean superset of Silverlight. MS is getting closer, but they're not there yet.
Ken Smith
+15  A: 

I don't know which version of Silverlight introduced it, but you now can. I'm using Silverlight 4 Beta.

   <data:DataGridTextColumn Header="Date" 
     Binding="{Binding CreateDt, StringFormat=\{0:d\}}" />

http://blog.davemdavis.net/2009/12/silverlight-4-data-binding-string.html

Here's info on Formatting Types, and more for DateTime.

Here's full documentation on Silverlight Binding.

Simon_Weaver
The relevant reference says only supported in Silverlight 4 http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat%28VS.95%29.aspx
russau
Incidentally, Tim Heuer points out a bug and workaround with the StringFormat binding when used with non-US cultures: http://timheuer.com/blog/archive/2010/08/11/stringformat-and-currentculture-in-silverlight.aspx
David White
A: 

The string format noted by Simon is in Silverlight 4. There is even support in Blend for setting the format.

Dave