alielie
2025-11-21 3a471ae651b228231bebb3a2d438baa1d4ac0261
HMI.UI/DpUtil/StackPanelHelper.cs
@@ -1,7 +1,7 @@
using 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);
                    }
                }
            }
        }
    }
}
}