DFLayoutBooleanFieldProperties.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. namespace InABox.Core
  2. {
  3. public enum DesignBooleanFieldType
  4. {
  5. Checkbox,
  6. ComboBox,
  7. Buttons
  8. }
  9. public class DFLayoutBooleanFieldProperties : DFLayoutFieldProperties<bool>
  10. {
  11. [EnumLookupEditor(typeof(DesignBooleanFieldType))]
  12. [EditorSequence(1)]
  13. public DesignBooleanFieldType Type { get; set; }
  14. [TextBoxEditor]
  15. [EditorSequence(2)]
  16. public string TrueValue { get; set; }
  17. [TextBoxEditor]
  18. [EditorSequence(3)]
  19. public string FalseValue { get; set; }
  20. protected override void LoadProperties()
  21. {
  22. base.LoadProperties();
  23. Type = GetProperty("Type", DesignBooleanFieldType.Checkbox);
  24. TrueValue = GetProperty("TrueValue", "True");
  25. FalseValue = GetProperty("FalseValue", "False");
  26. }
  27. protected override void SaveProperties()
  28. {
  29. base.SaveProperties();
  30. SetProperty("Type", Type);
  31. SetProperty("TrueValue", TrueValue);
  32. SetProperty("FalseValue", FalseValue);
  33. }
  34. public override string FormatValue(object? value)
  35. {
  36. return Equals(value, true) ? TrueValue : FalseValue;
  37. }
  38. public override object? ParseValue(object? value)
  39. {
  40. if (value is bool)
  41. return value;
  42. if (bool.TryParse(value as string, out var result))
  43. return result;
  44. if (value is string str)
  45. {
  46. if (str == TrueValue)
  47. return true;
  48. if (str == FalseValue)
  49. return false;
  50. return null;
  51. }
  52. return null;
  53. }
  54. }
  55. }