DFBooleanControl.cs 1.7 KB

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