From 0940f31b7eba1cf6b6a8c1fca72d97cf686e79f2 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 04 七月 2025 10:43:00 +0800
Subject: [PATCH] 优化插件,完善TreeListView

---
 HMI.UI/DpUtil/GridHelper.cs |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/HMI.UI/DpUtil/GridHelper.cs b/HMI.UI/DpUtil/GridHelper.cs
index d530c00..4b030a9 100644
--- a/HMI.UI/DpUtil/GridHelper.cs
+++ b/HMI.UI/DpUtil/GridHelper.cs
@@ -22,6 +22,19 @@
             DependencyProperty.RegisterAttached("Space", typeof(double), typeof(Grid),
                 new PropertyMetadata(0d, OnSpaceChanged));
 
+        public static string GetSpaceIgnore(DependencyObject obj)
+        {
+            return (string)obj.GetValue(SpaceIgnoreProperty);
+        }
+        public static void SetSpaceIgnore(DependencyObject obj, string value)
+        {
+            obj.SetValue(SpaceIgnoreProperty, value);
+        }
+
+        public static readonly DependencyProperty SpaceIgnoreProperty =
+            DependencyProperty.RegisterAttached("SpaceIgnore", typeof(string), typeof(Grid),
+                new PropertyMetadata(null, OnSpaceChanged));
+
         private static void OnSpaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             if (d is Grid grid)
@@ -49,8 +62,37 @@
                 return;
             }
 
+            bool ignoreTop = false, ignoreBottom = false, ignoreLeft = false, ignoreRight = false;
+            string baseData = GetSpaceIgnore(grid) ?? "";
+            if (baseData.Contains("ALL"))
+            {
+                ignoreTop = true;
+                ignoreBottom = true;
+                ignoreLeft = true;
+                ignoreRight = true;
+            }
+
+            if (baseData.Contains("LEFT"))
+            {
+                ignoreLeft = true;
+            }
+            if (baseData.Contains("RIGHT"))
+            {
+                ignoreRight = true;
+            }
+            if (baseData.Contains("TOP"))
+            {
+                ignoreTop = true;
+            }
+            if (baseData.Contains("BOTTOM"))
+            {
+                ignoreBottom = true;
+            }
+
             var rowCount = grid.RowDefinitions.Count;
+            rowCount = rowCount<= 0 ? 1 : rowCount;
             var columnCount = grid.ColumnDefinitions.Count;
+            columnCount = columnCount<=0 ? 1 : columnCount;
             int totalCount = rowCount * columnCount;
 
             if (grid.Children == null) return;
@@ -61,10 +103,17 @@
                 int rowSpan = Grid.GetRowSpan(cell);
                 int colSpan = Grid.GetColumnSpan(cell);
 
-                cell.Margin = new Thickness(space,
-                    space,
-                    (col + colSpan) == columnCount ? space : 0,
-                    (row + rowSpan) == rowCount ? space : 0);
+                double left = (col == 0 && ignoreLeft) ? 0 : space;
+                double top = (row == 0 && ignoreTop) ? 0 : space;
+
+                double right = ignoreRight ? 0 : ((col + colSpan == columnCount) ? space : 0);
+                double bottom = ignoreBottom ? 0 : ((row + rowSpan == rowCount) ? space : 0);
+                cell.Margin = new Thickness(left, top, right, bottom);
+
+                //cell.Margin = new Thickness(space,
+                //    space,
+                //    (col + colSpan) == columnCount ? space : 0,
+                //    (row + rowSpan) == rowCount ? space : 0);
             }
         }
         #endregion

--
Gitblit v1.9.1