Write date converter to join localization settings

If you use localization in your Windows Phone application and you need to show date in different formats, you need to built your own data converter to achieve this goal.

To reach this, you can write a DateTimeConverter class that implement IValueConverterInterface, as show below:

    public class DateTimeConverter : IValueConverter

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            if (value != null)
                string dateType = App.cultureInfo.DateTimeFormat.ShortDatePattern;
                return ((DateTime)value).ToString(dateType); // "dd.MM.yyyy"
                return String.Empty;

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            return DateTime.Parse(value.ToString());

Once you do this, you can go to the XAML file and do these 3 things:

  • define a namespace and use this to access you own date converter
  • put this snippet in the PhoneApplicationPage.Resources, like this
     <conv:DateTimeConverter x:Key="MyDateTimeToStringConverter">
  • in the textblock where you bind data you have to add these parameters
<TextBlock Text="{Binding Path=Date, Converter={StaticResource MyDateTimeToStringConverter}, ConverterParameter=\{0:M\}}" >

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s