9

Is there a way to format a date using XAML for Windows Phone 7?

If tried using:

<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" /> 

But I get the error:

The property 'StringFormat' was not found in type 'Binding'

3 Answers 3

20

Within SL4 this is possible...

<TextBlock Text="{Binding Date, StringFormat='MM/dd/yyyy'}}"/> 

...within SL3 you would need to make use of an IValueConverter.

public class DateTimeToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return String.Format("{0:MM/dd/yyyy}", (DateTime)value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

If you wanted a more robust approach you could make use of the ConverterParameter.

 public class DateTimeToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter == null) return ((DateTime)value).ToString(culture); else return ((DateTime)value).ToString(parameter as string, culture); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

Then in your XAML you would first define the converter as a resource...

<namespace:DateTimeToStringConverter x:Key="MyDateTimeToStringConverter"/> 

..then reference it along with an acceptable parameter for formatting the DateTime value...

<TextBlock Text="{Binding Date, Converter={StaticResource MyDateTimeToStringConverter}, ConverterParameter=\{0:M\}}"/> 
Sign up to request clarification or add additional context in comments.

8 Comments

+1 however some suggestions: Include a usage example, make use of the ConverterParameter to pass in an alternative format string, give the class a name more in keeping with its function say "DateTimeToStringConverter".
I would strongly recommend against using an IValueConverter. One line of C# code when exposing the property you're binding too would do this conversion just as well.
@JustinAngel Not a fan of pushing that type of functionality to the ViewModel when it is View specific functionality. I can't gather that the converter could possibly be that expensive to warrant pushing this to the ViewModel unless this is about approach versus performance.
It's been suggested use of Value Converters should be used judiciously as a consequence of performance impacts. In particular this is noted in the context of Listboxes. I appreciate there are competing priorities to balance here, which will weigh differently on different people and on different usages. One could argue that adopting use of converters on the phone eventually for some use will require you to concede to performance, and that a single approach may be prefered. At any rate, having both approaches noted as they are here is allright, and the reader can decide for their circumstance.
|
2

As far as I'm aware StringFromat is Silverlight 4 function, Silverlight for Windows Phone 7.0 is basically Silverlight 3 + some extras. I guess no then.

Comments

0

This is probably what you are looking for. RelativeDateTimeConverter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.