Browse Source

Added generic form of DynamicGridColumns so that it has a nicer interface.

Kenric Nugteren 1 year ago
parent
commit
4c0e85e7bc
1 changed files with 46 additions and 40 deletions
  1. 46 40
      inabox.wpf/DynamicGrid/DynamicGridColumns.cs

+ 46 - 40
inabox.wpf/DynamicGrid/DynamicGridColumns.cs

@@ -5,57 +5,63 @@ using System.Linq.Expressions;
 using InABox.Configuration;
 using InABox.Core;
 
-namespace InABox.DynamicGrid
-{
-    public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, IUserConfigurationSettings
+namespace InABox.DynamicGrid;
 
-    {
-        public string GridName { get; set; }
+public class DynamicGridColumns : List<DynamicGridColumn>, IGlobalConfigurationSettings, IUserConfigurationSettings
+{
+    public string GridName { get; set; }
 
-        private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
+    private static Dictionary<Type, List<DynamicGridColumn>> _columnscache = new Dictionary<Type, List<DynamicGridColumn>>();
 
-        public DynamicGridColumns ExtractColumns(Type type)
+    public DynamicGridColumns ExtractColumns(Type type)
+    {
+        if (!_columnscache.ContainsKey(type))
         {
-            if (!_columnscache.ContainsKey(type))
+            _columnscache[type] = new List<DynamicGridColumn>();
+            var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
+            foreach (var prop in properties)
             {
-                _columnscache[type] = new List<DynamicGridColumn>();
-                var properties = DatabaseSchema.Properties(type).OrderBy(x => x.Sequence);
-                foreach (var prop in properties)
+                if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
                 {
-                    if ((prop.Editor != null && !(prop.Editor is NullEditor)) || prop.Required)
+                    var col = new DynamicGridColumn
                     {
-                        var col = new DynamicGridColumn
-                        {
-                            ColumnName = prop.Name,
-                            Width = prop.Editor.Width,
-                            Alignment = prop.Editor.Alignment,
-                            Format = prop.Editor.Format,
-                            Editor = prop.Editor,
-                            Caption = prop.Caption
-                        };
-                        _columnscache[type].Add(col);
-                    }
+                        ColumnName = prop.Name,
+                        Width = prop.Editor.Width,
+                        Alignment = prop.Editor.Alignment,
+                        Format = prop.Editor.Format,
+                        Editor = prop.Editor,
+                        Caption = prop.Caption
+                    };
+                    _columnscache[type].Add(col);
                 }
             }
-            AddRange(_columnscache[type]);
-
-            return this;
         }
+        AddRange(_columnscache[type]);
+
+        return this;
+    }
 
-        public DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
-            Alignment alignment)
+    public DynamicGridColumn Add<TType, TProperty>(Expression<Func<TType, TProperty>> member, int width, string caption, string format,
+        Alignment alignment)
+    {
+        var name = CoreUtils.GetFullPropertyName(member, ".");
+        var result = new DynamicGridColumn
         {
-            var name = CoreUtils.GetFullPropertyName(member, ".");
-            var result = new DynamicGridColumn
-            {
-                ColumnName = name,
-                Caption = caption,
-                Width = width,
-                Format = format,
-                Alignment = alignment
-            };
-            Add(result);
-            return result;
-        }
+            ColumnName = name,
+            Caption = caption,
+            Width = width,
+            Format = format,
+            Alignment = alignment
+        };
+        Add(result);
+        return result;
+    }
+}
+
+public class DynamicGridColumns<T> : DynamicGridColumns
+{
+    public DynamicGridColumn Add<TProperty>(Expression<Func<T, TProperty>> member, int width, string caption, string format, Alignment alignment)
+    {
+        return Add<T, TProperty>(member, width, caption, format, alignment);
     }
 }