From dedcf4174c8540ee3a912daad2ff13e014e495b1 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 28 三月 2025 16:28:30 +0800
Subject: [PATCH] 优化UI组件;

---
 HMI.UI/DpUtil/StackPanelHelper.cs |   40 ++++++++++++++++++++++++++++------------
 1 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/HMI.UI/DpUtil/StackPanelHelper.cs b/HMI.UI/DpUtil/StackPanelHelper.cs
index 3c5c84a..ffe05eb 100644
--- a/HMI.UI/DpUtil/StackPanelHelper.cs
+++ b/HMI.UI/DpUtil/StackPanelHelper.cs
@@ -1,7 +1,7 @@
 锘縰sing System.Windows;
 using System.Windows.Controls;
 
-namespace HMI.UI
+namespace HMI.UI.DpUtil
 {
     public class StackPanelHelper
     {
@@ -20,19 +20,35 @@
             new PropertyMetadata(0d, (d, e) =>
             {
                 if (!(d is StackPanel sp)) return;
-                if (sp.Children == null) return;
-                var children = sp.Children;
-                double space = (double)e.NewValue;
-                if (sp.Orientation == Orientation.Horizontal)
+                sp.Loaded += Sp_Loaded;
+            }));
+
+        private static void Sp_Loaded(object sender, RoutedEventArgs e)
+        {
+            var sp = sender as StackPanel;
+            var children = sp.Children;
+            if (sp.Children == null) return;
+            double space = GetChildSpace(sp);
+            if (sp.Orientation == Orientation.Horizontal)
+            {
+                for (int i = 0; i < children.Count; i++)
                 {
-                    for (int i = 0; i < children.Count; i++)
+                    if (children[i] is FrameworkElement ui)
                     {
-                        if (children[i] is FrameworkElement ui)
-                        {
-                            ui.Margin = new Thickness(0, 0, (i == children.Count - 1) ? 0 : space, 0);
-                        }
+                        ui.Margin = new Thickness(0, 0, (i == children.Count - 1) ? 0 : space, 0);
                     }
                 }
-            }));
+            }
+            else
+            {
+                for (int i = 0; i < children.Count; i++)
+                {
+                    if (children[i] is FrameworkElement ui)
+                    {
+                        ui.Margin = new Thickness(0, 0, 0, (i == children.Count - 1) ? 0 : space);
+                    }
+                }
+            }
+        }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.1