using System;
using System.Globalization;
using System.Windows.Data;

namespace BeWo.Converter
{
    [ValueConversion(typeof(DateTime?), typeof(string))]
    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string s = string.Empty;
            DateTime? lDateTime = (DateTime?)value;
            if (lDateTime.HasValue)
            {
                s = lDateTime.Value.ToShortDateString();
            }

            return s;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime temp;
            DateTime.TryParse((string)value, out temp);
            if (temp != DateTime.MinValue)
            {
                return temp;
            }
            else
            {
                return null;
            }
        }
    }
}