DFBooleanControl.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using InABox.Core;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. namespace InABox.DynamicGrid;
  8. public class DFBooleanControl : DynamicFormFieldControl<DFLayoutBooleanField, DFLayoutBooleanFieldProperties, bool?, bool?>
  9. {
  10. private IOptionControl OptionControl = null!; // Late-initialised
  11. protected override FrameworkElement Create()
  12. {
  13. var options = new[] { Field.Properties.TrueValue, Field.Properties.FalseValue };
  14. var defValue = Field.Properties.Default switch
  15. {
  16. true => Field.Properties.TrueValue,
  17. false => Field.Properties.FalseValue,
  18. null => null
  19. };
  20. return Field.Properties.Type switch
  21. {
  22. DesignBooleanFieldType.Buttons => SetControl(new ButtonsOptionControl(options, ChangeField, defValue)),
  23. DesignBooleanFieldType.ComboBox => SetControl(new ComboBoxOptionControl(options, ChangeField, defValue)),
  24. DesignBooleanFieldType.Checkbox or _ => SetControl(new CheckBoxOptionControl(Field.Properties.TrueValue, Field.Properties.FalseValue, ChangeField, Field.Properties.Required, Field.Properties.Default)),
  25. };
  26. }
  27. private T SetControl<T>(T value)
  28. where T : FrameworkElement, IOptionControl
  29. {
  30. OptionControl = value;
  31. return value;
  32. }
  33. public override bool? GetSerializedValue()
  34. {
  35. return GetValue();
  36. }
  37. public override void SetSerializedValue(bool? value)
  38. {
  39. SetValue(value);
  40. }
  41. public override bool? GetValue()
  42. {
  43. var val = OptionControl.GetValue();
  44. if(val == Field.Properties.TrueValue)
  45. {
  46. return true;
  47. }
  48. else if(val == Field.Properties.FalseValue)
  49. {
  50. return false;
  51. }
  52. else
  53. {
  54. return null;
  55. }
  56. }
  57. public override void SetValue(bool? value) => OptionControl.SetValue(value switch
  58. {
  59. true => Field.Properties.TrueValue,
  60. false => Field.Properties.FalseValue,
  61. null => null
  62. });
  63. protected override bool IsEmpty() => OptionControl.IsEmpty();
  64. }