using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace OrderEntrySystem.Converters
{
    public class DoubleToStringConverter : IValueConverter
    {
        private string lastEnteredValue;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result = this.lastEnteredValue ?? value.ToString();
            this.lastEnteredValue = null;
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double doubleValue;
            string stringValue = value.ToString();
            if (double.TryParse(stringValue, out doubleValue))
            {
                this.lastEnteredValue = stringValue;
            }

            return doubleValue;
        }
    }
}
