alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace UILib.DpUtil
{
    public class GridHelper
    {
        #region Space
        public static double GetSpace(DependencyObject obj)
        {
            return (double)obj.GetValue(SpaceProperty);
        }
        public static void SetSpace(DependencyObject obj, double value)
        {
            obj.SetValue(SpaceProperty, value);
        }
 
        public static readonly DependencyProperty SpaceProperty =
            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)
            {
                if (e.Property == SpaceProperty)
                {
                    grid.Loaded += GridSpaceOnLoaded;
                    return;
                }
            }
        }
 
        private static void GridSpaceOnLoaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is Grid grid)) return;
            RefreshGridSpace(grid);
            grid.Loaded -= GridSpaceOnLoaded;
        }
 
        public static void RefreshGridSpace(Grid grid)
        {
            double space = GetSpace(grid);
            if (space <= 0)
            {
                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;
            foreach (FrameworkElement cell in grid.Children)
            {
                int row = Grid.GetRow(cell);
                int col = Grid.GetColumn(cell);
                int rowSpan = Grid.GetRowSpan(cell);
                int colSpan = Grid.GetColumnSpan(cell);
 
                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
 
        public static Brush GetInnerBorderBrush(Grid obj)
        {
            return (Brush)obj.GetValue(InnerBorderBrushProperty);
        }
        public static void SetInnerBorderBrush(Grid obj, Brush value)
        {
            obj.SetValue(InnerBorderBrushProperty, value);
        }
        public static readonly DependencyProperty InnerBorderBrushProperty =
            DependencyProperty.RegisterAttached("InnerBorderBrush", typeof(Brush), typeof(GridHelper),
                new PropertyMetadata(null, OnInnerBorderChanged));
 
        public static double GetInnerBorder(Grid obj)
        {
            var sb = obj.GetValue(InnerBorderProperty);
            return double.Parse(sb.ToString());
        }
 
        public static void SetInnerBorder(Grid obj, object value)
        {
            double dv = double.Parse(value.ToString());
            obj.SetValue(InnerBorderProperty, dv);
        }
 
        public static readonly DependencyProperty InnerBorderProperty =
            DependencyProperty.RegisterAttached("InnerBorder", typeof(object), typeof(GridHelper),
                new PropertyMetadata(0, OnInnerBorderChanged));
 
        public static string GetInnerBorderIgnore(Grid obj)
        {
            return (string)obj.GetValue(InnerBorderIgnoreProperty);
        }
        public static void SetInnerBorderIgnore(Grid obj, string value)
        {
            obj.SetValue(InnerBorderIgnoreProperty, value);
        }
 
        public static readonly DependencyProperty InnerBorderIgnoreProperty =
            DependencyProperty.RegisterAttached("InnerBorderIgnore", typeof(string),
                typeof(GridHelper), new PropertyMetadata("None", OnInnerBorderChanged));
 
        private static void OnInnerBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Grid grid)
            {
                if (e.Property == InnerBorderProperty || e.Property == InnerBorderIgnoreProperty)
                {
                    grid.Loaded += GridInnerBorder_Loaded;
                    return;
                }
            }
        }
 
        private static void GridInnerBorder_Loaded(object sender, RoutedEventArgs e)
        {
            Grid grid = sender as Grid;
            RefreshGridInnerBorder(grid);
            grid.Loaded -= GridInnerBorder_Loaded;
        }
 
        public static void RefreshGridInnerBorder(Grid grid)
        {
            double width = GetInnerBorder(grid);
            if (width <= 0) return;
            var rowCount = grid.RowDefinitions.Count;
            var columnCount = grid.ColumnDefinitions.Count;
            int totalCount = rowCount * columnCount;
 
            string baseData = GetInnerBorderIgnore(grid);
            HashSet<string> data = new HashSet<string>(baseData.Split(','));
            bool ignoreTop = data.Contains("All") || data.Contains("Top"),
                ignoreBottom = data.Contains("All") || data.Contains("Bottom"),
                ignoreLeft = data.Contains("All") || data.Contains("Left"),
                ignoreRight = data.Contains("All") || data.Contains("Right");
 
            foreach (var child in grid.Children)
            {
                if (child is Control control)
                {
                    Thickness bt;
                    Brush bb = GetInnerBorderBrush(grid) ?? Brushes.Black;
 
 
                    if (GetSpace(grid) > width)
                    {
                        bt = new Thickness(width);
                    }
                    else
                    {
                        int row = Grid.GetRow(control),
                           col = Grid.GetColumn(control),
                           rowSpan = Grid.GetRowSpan(control),
                           colSpan = Grid.GetColumnSpan(control);
 
                        double left = (col == 0 && ignoreLeft) ? 0 : width;
                        double top = (row == 0 && ignoreTop) ? 0 : width;
 
                        double right = ignoreRight ? 0 : ((col + colSpan == columnCount) ? width : 0);
                        double bottom = ignoreBottom ? 0 : ((row + rowSpan == rowCount) ? width : 0);
 
                        bt = new Thickness(left, top, right, bottom);
                    }
                    control.BorderBrush = bb;
                    control.BorderThickness = bt;
                }
            }
        }
    }
}