DFBooleanControl.cs 1.9 KB

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