DFBooleanControl.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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>
  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 void Deserialize(string serialized) => SetValue(String.Equals(serialized,Field.Properties.TrueValue));
  32. public override string Serialize() => GetValue()
  33. ? Field.Properties.TrueValue
  34. : Field.Properties.FalseValue;
  35. public override bool GetValue() => OptionControl.GetValue() == Field.Properties.TrueValue;
  36. public override void SetValue(bool value) => OptionControl.SetValue(value ? Field.Properties.TrueValue : Field.Properties.FalseValue);
  37. protected override bool IsEmpty() => Field.Properties.Type switch
  38. {
  39. DesignBooleanFieldType.Checkbox => GetValue() != true,
  40. _ => OptionControl.IsEmpty()
  41. };
  42. }
  43. }