chr
2025-02-24 104cad5ee53eb05d3a89d508a029425726ae89ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.UI
{
    public class StackPanelHelper
    {
        public static double GetChildSpace(StackPanel sp)
        {
            return (double)sp.GetValue(ChildSpaceProperty);
        }
 
        public static void SetChildSpace(StackPanel sp, double value)
        {
            sp.SetValue(ChildSpaceProperty, value);
        }
 
        public static readonly DependencyProperty ChildSpaceProperty =
            DependencyProperty.RegisterAttached("ChildSpace", typeof(double), typeof(StackPanelHelper),
            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)
                {
                    for (int i = 0; i < children.Count; i++)
                    {
                        if (children[i] is FrameworkElement ui)
                        {
                            ui.Margin = new Thickness(0, 0, (i == children.Count - 1) ? 0 : space, 0);
                        }
                    }
                }
            }));
    }
}