Просмотр исходного кода

CurrencyEditor now only works with decimals, and added DecimalEditorControl to handle decimal types for DoubleEditor

Kenric Nugteren 2 месяцев назад
Родитель
Сommit
df5a109f37

+ 0 - 1
InABox.Core/CoreUtils.cs

@@ -1434,7 +1434,6 @@ namespace InABox.Core
                 return false;
 
             return dataType == typeof(double)
-                   || dataType == typeof(float)
                    || dataType == typeof(float);
         }
 

+ 3 - 0
InABox.Core/Objects/Editors/DoubleEditor.cs

@@ -1,5 +1,8 @@
 namespace InABox.Core
 {
+    /// <summary>
+    /// For use with either <see langword="double"/> or <see langword="decimal"/>.
+    /// </summary>
     public class DoubleEditor : NumericEditor
     {
         public DoubleEditor(int digits) : base(digits)

+ 1 - 1
InABox.Core/Objects/Editors/Utils/EditorTypeUtils.cs

@@ -24,7 +24,7 @@ namespace InABox.Core
                 editor = new DurationEditor();
             else if (type == typeof(string))
                 editor = new TextBoxEditor();
-            else if (type.IsFloatingPoint())
+            else if (type.IsFloatingPoint() || type == typeof(decimal))
                 editor = new DoubleEditor();
             else if (type.IsOrdinal())
                 editor = new IntegerEditor();

+ 8 - 6
inabox.wpf/DynamicGrid/Editors/CurrencyEditor/CurrencyEditorControl.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics.CodeAnalysis;
 using System.Linq;
 using System.Windows;
 using System.Windows.Controls;
@@ -8,7 +9,7 @@ using Syncfusion.Windows.Shared;
 
 namespace InABox.DynamicGrid
 {
-    public class CurrencyEditorControl : DynamicEditorControl<double, CurrencyEditor>
+    public class CurrencyEditorControl : DynamicEditorControl<decimal, CurrencyEditor>
     {
         
         static CurrencyEditorControl()
@@ -16,7 +17,7 @@ namespace InABox.DynamicGrid
             //DynamicEditorControlFactory.Register<CurrencyEditorControl, CurrencyEditor>();
         }
         
-        private CurrencyTextBox? Editor;
+        private CurrencyTextBox Editor = null!; // Initialised in Create
         
         private String _currencySymbol = "";
 
@@ -41,6 +42,7 @@ namespace InABox.DynamicGrid
         {
         }
 
+        [MemberNotNull(nameof(Editor))]
         protected override FrameworkElement CreateEditor()
         {
             var DockPanel = new DockPanel
@@ -98,16 +100,16 @@ namespace InABox.DynamicGrid
             return result;
         }
 
-        protected override double RetrieveValue()
+        protected override decimal RetrieveValue()
         {
-            return Convert.ToDouble(Editor.Value);
+            return Editor.Value ?? 0;
         }
 
-        protected override void UpdateValue(double value)
+        protected override void UpdateValue(decimal value)
         {
             try
             {
-                Editor.Value = Convert.ToDecimal(value);
+                Editor.Value = value;
             }
             catch (Exception e)
             {

+ 69 - 0
inabox.wpf/DynamicGrid/Editors/DecimalEditor/DecimalEditorControl.cs

@@ -0,0 +1,69 @@
+using System;
+using System.Windows;
+using System.Windows.Media;
+using InABox.Core;
+using Syncfusion.Windows.Shared;
+
+namespace InABox.DynamicGrid;
+
+public class DecimalEditorControl : DynamicEditorControl<decimal, DoubleEditor>
+{
+    private DoubleTextBox Editor;
+
+    public DecimalEditorControl()
+    {
+        Width = 150;
+    }
+
+    protected override FrameworkElement CreateEditor()
+    {
+        var def = EditorDefinition as DoubleEditor;
+        var digits = def != null ? def.Digits : 2;
+
+        Editor = new DoubleTextBox
+        {
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            NumberDecimalDigits = digits
+        };
+        Editor.ValueChanged += (o, e) => CheckChanged();
+        return Editor;
+    }
+
+    public override void Configure()
+    {
+        Editor.NumberDecimalDigits = EditorDefinition.Digits;
+    }
+
+    public override int DesiredHeight()
+    {
+        return 25;
+    }
+
+    public override int DesiredWidth()
+    {
+        return 150;
+    }
+
+    protected override decimal RetrieveValue()
+    {
+        return Editor.Value.HasValue ? Math.Round(Convert.ToDecimal(Editor.Value.Value), EditorDefinition.Digits) : default;
+    }
+
+    protected override void UpdateValue(decimal value)
+    {
+        Editor.Value = Convert.ToDouble(value);
+    }
+
+    public override void SetFocus()
+    {
+        Editor.Focus();
+    }
+
+    public override void SetColor(Color color)
+    {
+        Editor.Background = new SolidColorBrush(color);
+    }
+}

+ 54 - 55
inabox.wpf/DynamicGrid/Editors/DoubleEditor/DoubleEditorControl.cs

@@ -3,72 +3,71 @@ using System.Windows.Media;
 using InABox.Core;
 using Syncfusion.Windows.Shared;
 
-namespace InABox.DynamicGrid
+namespace InABox.DynamicGrid;
+
+public class DoubleEditorControl : DynamicEditorControl<double, DoubleEditor>
 {
-    public class DoubleEditorControl : DynamicEditorControl<double, DoubleEditor>
+    static DoubleEditorControl()
     {
-        static DoubleEditorControl()
-        {
-            //DynamicEditorControlFactory.Register<DoubleEditorControl, DoubleEditor>();
-        }
-        
-        private DoubleTextBox Editor;
-
-        public DoubleEditorControl()
-        {
-            Width = 150;
-        }
+        //DynamicEditorControlFactory.Register<DoubleEditorControl, DoubleEditor>();
+    }
+    
+    private DoubleTextBox Editor;
 
-        protected override FrameworkElement CreateEditor()
-        {
-            var def = EditorDefinition as DoubleEditor;
-            var digits = def != null ? def.Digits : 2;
+    public DoubleEditorControl()
+    {
+        Width = 150;
+    }
 
-            Editor = new DoubleTextBox
-            {
-                VerticalAlignment = VerticalAlignment.Stretch,
-                VerticalContentAlignment = VerticalAlignment.Center,
-                HorizontalAlignment = HorizontalAlignment.Stretch,
-                HorizontalContentAlignment = HorizontalAlignment.Center,
-                NumberDecimalDigits = digits
-            };
-            Editor.ValueChanged += (o, e) => CheckChanged();
-            return Editor;
-        }
+    protected override FrameworkElement CreateEditor()
+    {
+        var def = EditorDefinition as DoubleEditor;
+        var digits = def != null ? def.Digits : 2;
 
-        public override void Configure()
+        Editor = new DoubleTextBox
         {
-            Editor.NumberDecimalDigits = EditorDefinition.Digits;
-        }
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            NumberDecimalDigits = digits
+        };
+        Editor.ValueChanged += (o, e) => CheckChanged();
+        return Editor;
+    }
 
-        public override int DesiredHeight()
-        {
-            return 25;
-        }
+    public override void Configure()
+    {
+        Editor.NumberDecimalDigits = EditorDefinition.Digits;
+    }
 
-        public override int DesiredWidth()
-        {
-            return 150;
-        }
+    public override int DesiredHeight()
+    {
+        return 25;
+    }
 
-        protected override double RetrieveValue()
-        {
-            return Editor.Value.HasValue ? Editor.Value.Value : default;
-        }
+    public override int DesiredWidth()
+    {
+        return 150;
+    }
 
-        protected override void UpdateValue(double value)
-        {
-            Editor.Value = value;
-        }
+    protected override double RetrieveValue()
+    {
+        return Editor.Value.HasValue ? Editor.Value.Value : default;
+    }
 
-        public override void SetFocus()
-        {
-            Editor.Focus();
-        }
+    protected override void UpdateValue(double value)
+    {
+        Editor.Value = value;
+    }
 
-        public override void SetColor(Color color)
-        {
-            Editor.Background = new SolidColorBrush(color);
-        }
+    public override void SetFocus()
+    {
+        Editor.Focus();
+    }
+
+    public override void SetColor(Color color)
+    {
+        Editor.Background = new SolidColorBrush(color);
     }
 }