DFLayoutTimeFieldProperties.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public class DFLayoutTimeFieldProperties : DFLayoutFieldProperties<TimeSpan>
  5. {
  6. public DFLayoutTimeFieldProperties()
  7. {
  8. Format = "hh:mm";
  9. }
  10. public string Format { get; set; }
  11. public override string FormatValue(object value)
  12. {
  13. var time = new TimeSpan();
  14. if (value != null)
  15. {
  16. if (value is TimeSpan)
  17. time = (TimeSpan)value;
  18. else if (value is string)
  19. TimeSpan.TryParse(value as string, out time);
  20. }
  21. return string.IsNullOrWhiteSpace(Format) || string.Equals(Format, "hh:mm")
  22. ? Math.Truncate(time.TotalHours).ToString("#00") + ":" + time.Minutes.ToString("D2")
  23. : string.Format("{0:" + Format.Replace(":", "\\:") + "}", time);
  24. }
  25. public override object ParseValue(object value)
  26. {
  27. if (value is TimeSpan)
  28. return value;
  29. if (TimeSpan.TryParse(value as string, out var result))
  30. return result;
  31. return false;
  32. }
  33. protected override void LoadProperties()
  34. {
  35. base.LoadProperties();
  36. Format = GetProperty("Format", "hh:mm");
  37. }
  38. protected override void SaveProperties()
  39. {
  40. base.SaveProperties();
  41. SetProperty("Format", Format);
  42. }
  43. }
  44. }