Browse Source

Made TextBoxEditors update changed when typing, rather than tabbing off

Kenric Nugteren 7 months ago
parent
commit
4c8b7f952d

+ 94 - 95
inabox.wpf/DynamicGrid/Editors/CodeEditor/CodeEditorControl.cs

@@ -6,122 +6,121 @@ using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
 using InABox.Core;
 using InABox.Core;
 
 
-namespace InABox.DynamicGrid
+namespace InABox.DynamicGrid;
+
+public class CodeEditorControl : DynamicEditorControl<string, BaseCodeEditor>
 {
 {
-    public class CodeEditorControl : DynamicEditorControl<string, BaseCodeEditor>
+    
+    static CodeEditorControl()
     {
     {
-        
-        static CodeEditorControl()
-        {
-            //DynamicEditorControlFactory.Register<CodeEditorControl, BaseCodeEditor, CodeEditor>();
-            //DynamicEditorControlFactory.Register<CodeEditorControl, BaseCodeEditor, UniqueCodeEditor>();
-        }
-        
-        private TextBox Editor;
+        //DynamicEditorControlFactory.Register<CodeEditorControl, BaseCodeEditor, CodeEditor>();
+        //DynamicEditorControlFactory.Register<CodeEditorControl, BaseCodeEditor, UniqueCodeEditor>();
+    }
+    
+    private TextBox Editor;
 
 
-        public override void Configure()
-        {
-        }
+    public override void Configure()
+    {
+    }
 
 
-        protected override FrameworkElement CreateEditor()
+    protected override FrameworkElement CreateEditor()
+    {
+        var dock = new DockPanel
         {
         {
-            var dock = new DockPanel
-            {
-                HorizontalAlignment = HorizontalAlignment.Stretch,
-                VerticalAlignment = VerticalAlignment.Stretch
-            };
-
-            var buttons = CreateButtons(out var DisableEditor);
-            foreach (var button in buttons)
-            {
-                button.SetValue(DockPanel.DockProperty, Dock.Right);
-                dock.Children.Add(button);
-                dock.Width += button.Width + 5;
-            }
-
-            Editor = new TextBox
-            {
-                CharacterCasing = CharacterCasing.Upper,
-                VerticalContentAlignment = VerticalAlignment.Center
-            };
-            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
-            if (DisableEditor)
-            {
-                Editor.Background = new SolidColorBrush(Colors.Silver);
-                Editor.IsEnabled = false;
-            }
-
-            Editor.TextChanged += (o, e) => { CheckChanged(); };
-
-            Editor.PreviewTextInput += Editor_PreviewTextInput;
-            DataObject.AddPastingHandler(Editor, Editor_TextPaste);
-
-            dock.Children.Add(Editor);
-
-            return dock;
-        }
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            VerticalAlignment = VerticalAlignment.Stretch
+        };
 
 
-        private void Editor_TextPaste(object sender, DataObjectPastingEventArgs e)
+        var buttons = CreateButtons(out var DisableEditor);
+        foreach (var button in buttons)
         {
         {
-            if (e.DataObject.GetDataPresent(typeof(string)))
-            {
-                var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
-                var text = (string)e.DataObject.GetData(typeof(string));
-                if (!IsTextAllowed(text, allowable))
-                    e.CancelCommand();
-            }
-            else
-            {
-                e.CancelCommand();
-            }
+            button.SetValue(DockPanel.DockProperty, Dock.Right);
+            dock.Children.Add(button);
+            dock.Width += button.Width + 5;
         }
         }
 
 
-        private bool IsTextAllowed(string text, string allowable)
+        Editor = new TextBox
+        {
+            CharacterCasing = CharacterCasing.Upper,
+            VerticalContentAlignment = VerticalAlignment.Center
+        };
+        Editor.SetValue(DockPanel.DockProperty, Dock.Left);
+        if (DisableEditor)
         {
         {
-            if (string.IsNullOrWhiteSpace(allowable))
-                return true;
-            return Array.TrueForAll(text.ToCharArray(), delegate(char c) { return allowable.Contains(c); });
+            Editor.Background = new SolidColorBrush(Colors.Silver);
+            Editor.IsEnabled = false;
         }
         }
 
 
-        private void Editor_PreviewTextInput(object sender, TextCompositionEventArgs e)
+        Editor.TextChanged += (o, e) => { CheckChanged(); };
+
+        Editor.PreviewTextInput += Editor_PreviewTextInput;
+        DataObject.AddPastingHandler(Editor, Editor_TextPaste);
+
+        dock.Children.Add(Editor);
+
+        return dock;
+    }
+
+    private void Editor_TextPaste(object sender, DataObjectPastingEventArgs e)
+    {
+        if (e.DataObject.GetDataPresent(typeof(string)))
         {
         {
             var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
             var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
-            e.Handled = !IsTextAllowed(e.Text, allowable);
+            var text = (string)e.DataObject.GetData(typeof(string));
+            if (!IsTextAllowed(text, allowable))
+                e.CancelCommand();
         }
         }
-
-        public override int DesiredHeight()
+        else
         {
         {
-            return 25;
+            e.CancelCommand();
         }
         }
+    }
 
 
-        public override int DesiredWidth()
-        {
-            var result = 150;
-            var btnEditor = EditorDefinition as IButtonEditor;
-            if (btnEditor?.Buttons != null)
-                foreach (var button in ((IButtonEditor)EditorDefinition).Buttons)
-                    result += button.Width + 5;
-            return result;
-        }
+    private bool IsTextAllowed(string text, string allowable)
+    {
+        if (string.IsNullOrWhiteSpace(allowable))
+            return true;
+        return Array.TrueForAll(text.ToCharArray(), delegate(char c) { return allowable.Contains(c); });
+    }
 
 
-        protected override string RetrieveValue()
-        {
-            return Editor.Text;
-        }
+    private void Editor_PreviewTextInput(object sender, TextCompositionEventArgs e)
+    {
+        var allowable = (EditorDefinition as BaseCodeEditor)?.ValidChars;
+        e.Handled = !IsTextAllowed(e.Text, allowable);
+    }
 
 
-        protected override void UpdateValue(string value)
-        {
-            Editor.Text = value;
-        }
+    public override int DesiredHeight()
+    {
+        return 25;
+    }
 
 
-        public override void SetFocus()
-        {
-            Editor.Focus();
-        }
+    public override int DesiredWidth()
+    {
+        var result = 150;
+        var btnEditor = EditorDefinition as IButtonEditor;
+        if (btnEditor?.Buttons != null)
+            foreach (var button in ((IButtonEditor)EditorDefinition).Buttons)
+                result += button.Width + 5;
+        return result;
+    }
 
 
-        public override void SetColor(Color color)
-        {
-            Editor.Background = new SolidColorBrush(color);
-        }
+    protected override string RetrieveValue()
+    {
+        return Editor.Text;
+    }
+
+    protected override void UpdateValue(string value)
+    {
+        Editor.Text = value;
+    }
+
+    public override void SetFocus()
+    {
+        Editor.Focus();
+    }
+
+    public override void SetColor(Color color)
+    {
+        Editor.Background = new SolidColorBrush(color);
     }
     }
 }
 }

+ 1 - 1
inabox.wpf/DynamicGrid/Editors/DurationEditor/DurationEditorControl.cs

@@ -105,7 +105,7 @@ namespace InABox.DynamicGrid
             Editor.ValueChanged += (o, e) =>
             Editor.ValueChanged += (o, e) =>
             {
             {
                 IsChanged = true;
                 IsChanged = true;
-                //CheckChanged();
+                CheckChanged();
             };
             };
 
 
             Editor.LostFocus += (o, e) =>
             Editor.LostFocus += (o, e) =>

+ 1 - 1
inabox.wpf/DynamicGrid/Editors/MemoEditor/MemoEditorControl.cs

@@ -36,7 +36,7 @@ namespace InABox.DynamicGrid
             Editor.TextChanged += (o, e) =>
             Editor.TextChanged += (o, e) =>
             {
             {
                 IsChanged = true;
                 IsChanged = true;
-                //CheckChanged();
+                CheckChanged();
             };
             };
             Editor.LostFocus += (o, e) =>
             Editor.LostFocus += (o, e) =>
             {
             {

+ 4 - 2
inabox.wpf/DynamicGrid/Editors/TextBoxEditor/TextBoxEditorControl.cs

@@ -55,9 +55,11 @@ public class TextBoxEditorControl : DynamicEditorControl<string, TextBoxEditor>
 
 
         Editor.TextChanged += (o, e) =>
         Editor.TextChanged += (o, e) =>
         {
         {
-            if(Loaded)
+            if (Loaded)
+            {
                 IsChanged = true;
                 IsChanged = true;
-            //CheckChanged();
+                CheckChanged();
+            }
         };
         };
         Editor.LostFocus += (o, e) =>
         Editor.LostFocus += (o, e) =>
         {
         {

+ 1 - 1
inabox.wpf/DynamicGrid/Editors/TimeOfDayEditor/TimeOfDayEditorControl.cs

@@ -133,7 +133,7 @@ namespace InABox.DynamicGrid
             Editor.DateTimeChanged += (o, e) =>
             Editor.DateTimeChanged += (o, e) =>
             {
             {
                 IsChanged = true;
                 IsChanged = true;
-                //CheckChanged();
+                CheckChanged();
             };
             };
 
 
             Editor.LostFocus += (o, e) =>
             Editor.LostFocus += (o, e) =>