DFBooleanControl.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 bool GetValue() => OptionControl.GetValue() == Field.Properties.TrueValue;
  32. public override void SetValue(bool value) => OptionControl.SetValue(value ? Field.Properties.TrueValue : Field.Properties.FalseValue);
  33. protected override bool IsEmpty() => Field.Properties.Type switch
  34. {
  35. DesignBooleanFieldType.Checkbox => GetValue() != true,
  36. _ => OptionControl.IsEmpty()
  37. };
  38. }
  39. }