0
votes

I am using converter in windows store app in twoway mode.

public object ConvertBack(object value, Type targetType,
        object parameter, string language)
{
    return ((DateTime)value).ToString("yyyy-MM-dd");
}

Another approach:

public object ConvertBack(object value, Type targetType,
        object parameter, string language)
{
    DateTime dt;
    DateTime.TryParseExact(value.ToString(),
                           "yyyy-MM-dd",
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None,
                           out dt);
    return dt;
}

Can anyone tell me why this does not work for me? And afkors, how to solve it? From datePicker converter gets this string: 19.1.2014 15:43:02 +01:00 and is unable to conver.

Error message: Converter failed to convert value of type 'System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' to type 'DateTime'; BindingExpression: Path='DateFrom' DataItem='Infomed21_Mbx.Data.resultFilter, Infomed21-Mbx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.DatePicker' (Name='datePicker_from'); target property is 'Date' (type 'DateTime')

2

2 Answers

0
votes

DateTime doesn't know what to do with that string. Try using DateTime.ParseExact a explicitly set the expected string format.

0
votes

Here are my DateTime converters I use in WP8 project. They should work on W8 without any trouble

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string strValue = value as string;
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return System.Windows.DependencyProperty.UnsetValue;
    }