Sfoglia il codice sorgente

Made DF boolean fields able to be null and fixed default values for options field controls

Kenric Nugteren 1 mese fa
parent
commit
429a71b0c5

+ 25 - 9
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFBooleanFieldControl.cs

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
 
 namespace PRS.Avalonia.DigitalForms;
 
-class DFBooleanFieldControl : DigitalFormFieldControl<DFLayoutBooleanField, DFLayoutBooleanFieldProperties, bool, bool?>
+class DFBooleanFieldControl : DigitalFormFieldControl<DFLayoutBooleanField, DFLayoutBooleanFieldProperties, bool?, bool?>
 {
     private IOptionControl OptionControl = null!; // Late-initialised
 
@@ -33,18 +33,34 @@ class DFBooleanFieldControl : DigitalFormFieldControl<DFLayoutBooleanField, DFLa
 
     public override void SetSerializedValue(bool? value)
     {
-        SetValue(value ?? false);
+        SetValue(value);
     }
 
-    public override bool GetValue() => OptionControl.GetValue() == Field.Properties.TrueValue;
-
-    public override void SetValue(bool value) => OptionControl.SetValue(value ? Field.Properties.TrueValue : Field.Properties.FalseValue);
+    public override bool? GetValue()
+    {
+        var val = OptionControl.GetValue();
+        if(val == Field.Properties.TrueValue)
+        {
+            return true;
+        }
+        else if(val == Field.Properties.FalseValue)
+        {
+            return false;
+        }
+        else
+        {
+            return null;
+        }
+    }
 
-    protected override bool IsEmpty() => Field.Properties.Type switch
+    public override void SetValue(bool? value) => OptionControl.SetValue(value switch
     {
-        DesignBooleanFieldType.Checkbox => GetValue() != true,
-        _ => OptionControl.IsEmpty()
-    };
+        true => Field.Properties.TrueValue,
+        false => Field.Properties.FalseValue,
+        null => null
+    });
+
+    protected override bool IsEmpty() => OptionControl.IsEmpty();
 
     public override void SetBackground(IBrush brush, bool defaultColour)
     {

+ 4 - 4
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFOptionFieldControl.cs

@@ -19,7 +19,7 @@ internal interface IOptionControl
     IBrush? Background { get; set; }
 
     public string? GetValue();
-    public void SetValue(string value);
+    public void SetValue(string? value);
 
     public bool IsEmpty();
 }
@@ -72,7 +72,7 @@ public class ButtonsOptionControl : ContentControl, IOptionControl
         return _strip.SelectedItem as string;
     }
 
-    public void SetValue(string value)
+    public void SetValue(string? value)
     {
         var item = _strip.Items.FirstOrDefault(x => Equals(x.Tag, value));
         _strip.SelectedIndex = item is not null ? _strip.Items.IndexOf(item) : -1;
@@ -129,7 +129,7 @@ public class RadioOptionControl : ContentControl, IOptionControl
         return null;
     }
 
-    public void SetValue(string value)
+    public void SetValue(string? value)
     {
         foreach (var child in _grid.Children)
         {
@@ -185,7 +185,7 @@ public class ComboBoxOptionControl : ContentControl, IOptionControl
         return _comboBox.SelectedValue as string;
     }
 
-    public void SetValue(string value)
+    public void SetValue(string? value)
     {
         _comboBox.SelectedValue = value;
     }