TimeSpanConverter.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Data;
  6. using System.Windows.Media;
  7. using Xceed.Wpf.DataGrid;
  8. using System.Data;
  9. using System.Windows;
  10. namespace Aitex.UI.RecipeEditor
  11. {
  12. public class TimeSpanConverter : IValueConverter
  13. {
  14. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  15. {
  16. try
  17. {
  18. string v = (string)value;
  19. DateTime dt;
  20. if (!DateTime.TryParse(v, out dt))
  21. return new DateTime(0);
  22. dt = new DateTime(1, 1, 1, dt.Hour, dt.Minute, dt.Second);
  23. return dt;
  24. }
  25. catch (Exception ex)
  26. {
  27. System.Diagnostics.Debug.WriteLine(ex.Message);
  28. }
  29. return new DateTime(0);
  30. }
  31. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  32. {
  33. try
  34. {
  35. DateTime dt = (DateTime)value;
  36. return string.Format("{0}:{1}:{2}", dt.Hour.ToString("00"), dt.Minute.ToString("00"), dt.Second.ToString("00"));
  37. }
  38. catch (Exception ex)
  39. {
  40. System.Diagnostics.Debug.WriteLine(ex.Message);
  41. }
  42. return "00:00:00";
  43. }
  44. }
  45. }