DFLayoutDateFieldProperties.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Globalization;
  3. namespace InABox.Core
  4. {
  5. public class DFLayoutDateFieldProperties : DFLayoutFieldProperties<DateTime, DateTime?>
  6. {
  7. [EditorSequence(-995)]
  8. [DateEditor]
  9. public override DateTime Default { get; set; }
  10. public DFLayoutDateFieldProperties()
  11. {
  12. Format = "dd MMM yy";
  13. }
  14. public string Format { get; set; }
  15. public override string FormatValue(DateTime? value)
  16. {
  17. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format.Replace(":", "\\:")) + "}", value);
  18. }
  19. public override DateTime GetValue(DateTime? value)
  20. {
  21. return value ?? Default;
  22. }
  23. public override void SerializeValue(DFSaveStorageEntry entry, DateTime? value)
  24. {
  25. if(value != null)
  26. {
  27. entry.SetValue(value.Value.ToString("dd-MM-yyyy"));
  28. }
  29. }
  30. public override DateTime? DeserializeValue(DFLoadStorageEntry entry)
  31. {
  32. var value = entry.GetValue();
  33. if (value is DateTime date)
  34. return date;
  35. if (DateTime.TryParseExact(value as string, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
  36. return result;
  37. return null;
  38. }
  39. protected override void LoadProperties()
  40. {
  41. base.LoadProperties();
  42. Format = GetProperty("Format", Format);
  43. }
  44. protected override void SaveProperties()
  45. {
  46. base.SaveProperties();
  47. SetProperty("Format", Format);
  48. }
  49. }
  50. }