Are WP7 dates not internationalized?

I have an application in which resources are localized correctly. However, the datetime databetime binding always shows the use of formatting in the USA.

I checked in the App class at startup time, and the expected culture is set for CurrentCulture and CurrentUICulture.

I don't have date formatting as far as I know.

How do I get dates formatted in the current culture?

+4
source share
3 answers

It turns out there is a very simple solution.

By adding an IValueConverter and using the converter in the binding expression, but ignoring the culture argument, formatting works fine.
You will need one converter (if you do not accept it) for each other format.

Converter (removed the attribute from the sample):

public class DateConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime date = (DateTime)value; return date.ToShortDateString(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string strValue = value as string; DateTime resultDateTime; if (DateTime.TryParse(strValue, out resultDateTime)) { return resultDateTime; } return DependencyProperty.UnsetValue; } } 

Namespace

 xmlns:conv="clr-namespace:Sjofartsverket.LotsPDA20.Client.Converters" 

resource

 <conv:DateConverter x:Key="dateConverter" /> 

Expression Binding:

 <TextBlock Text="{Binding StartDate, Converter={StaticResource dateConverter}}" 

Result: The date is displayed in the correct culture.

+1
source

You need to change the data type of the StartDate property to a string:

 string _startDate; public string StartDate { get { return _startDate; } set { _startDate = value; OnPropertyChanged("StartDate"); } } 

When you assign a StartDate value, use one of the following overloads of the ToString() method, for convenience:

 StartDate = DateTime.Now.ToString(); StartDate = DateTime.Now.ToString("d"); StartDate = DateTime.Now.ToString("D"); 
+1
source

Instead of passing the DateTime to the view and relying on the binding to convert it to the correct format, create an additional property that wraps the existing one but applies the appropriate conversion / formatting.
eg.

 public class MyViewModel { public DateTime StartDate { get; set; } public string LocalizedStartDate { get { return this.StartDate.ToString(CultureInfo.CurrentUICulture); } } } 

and then bind:

 <TextBlock Text="{Binding LocalizedStartDate}" .... /> 
+1
source

All Articles