DFBooleanControl.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. {
  9. public class DFBooleanControl : DynamicFormFieldControl<DFLayoutBooleanField, DFLayoutBooleanFieldProperties, bool, bool?>
  10. {
  11. private IOptionControl OptionControl = null!; // Late-initialised
  12. protected override FrameworkElement Create()
  13. {
  14. var options = new[] { Field.Properties.TrueValue, Field.Properties.FalseValue };
  15. switch (Field.Properties.Type)
  16. {
  17. case DesignBooleanFieldType.Buttons:
  18. return SetControl(new ButtonsOptionControl(options, ChangeField));
  19. case DesignBooleanFieldType.ComboBox:
  20. return SetControl(new ComboBoxOptionControl(options, ChangeField));
  21. }
  22. return SetControl(new CheckBoxOptionControl(Field.Properties.TrueValue, Field.Properties.FalseValue, ChangeField));
  23. }
  24. private T SetControl<T>(T value)
  25. where T : FrameworkElement, IOptionControl
  26. {
  27. OptionControl = value;
  28. return value;
  29. }
  30. public override bool? GetSerializedValue()
  31. {
  32. return GetValue();
  33. }
  34. public override void SetSerializedValue(bool? value)
  35. {
  36. SetValue(value ?? false);
  37. }
  38. public override bool GetValue() => OptionControl.GetValue() == Field.Properties.TrueValue;
  39. public override void SetValue(bool value) => OptionControl.SetValue(value ? Field.Properties.TrueValue : Field.Properties.FalseValue);
  40. protected override bool IsEmpty() => Field.Properties.Type switch
  41. {
  42. DesignBooleanFieldType.Checkbox => GetValue() != true,
  43. _ => OptionControl.IsEmpty()
  44. };
  45. }
  46. }