alielie
2025-06-12 c1c3772a11b1f3fbad8846cc714120771d85fa53
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
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
 
namespace Plugin.TestResult_WPF
{
    public class GridProperties
    {
        public static bool GetShowBorder(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowBorderProperty);
        }
        public static void SetShowBorder(DependencyObject obj, bool value)
        {
            obj.SetValue(ShowBorderProperty, value);
        }
        public static readonly DependencyProperty ShowBorderProperty =
            DependencyProperty.RegisterAttached("ShowBorder", typeof(bool), typeof(Grid), new PropertyMetadata(false, OnShowBorderChanged));
 
        public static int GetBorderSpace(DependencyObject obj)
        {
            return (int)obj.GetValue(BorderSpaceProperty);
        }
        public static void SetBorderSpace(DependencyObject obj, int value)
        {
            obj.SetValue(BorderSpaceProperty, value);
        }
        public static readonly DependencyProperty BorderSpaceProperty =
            DependencyProperty.RegisterAttached("BorderSpace", typeof(int), typeof(Grid), new PropertyMetadata(0, OnShowBorderChanged));
 
 
 
        private static void OnShowBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Grid grid)
            {
                if (e.Property == ShowBorderProperty)
                {
                    grid.Loaded += GridInnerBorderLoaded;
                    return;
                }
                else if (e.Property == BorderSpaceProperty)
                {
                    grid.Loaded += GridSpaceOnLoaded;
                    return;
                }
            }
            else if (d is UniformGrid uniformGrid)
            {
                uniformGrid.Loaded += UniformGridOnLoad;
                return;
            }
        }
 
        private static void GridInnerBorderLoaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is Grid grid)) return;
            var rowCount = grid.RowDefinitions.Count;
            var columnCount = grid.ColumnDefinitions.Count;
            int totalCount = rowCount * columnCount;
 
            int width = 1;
 
            string baseData = "None";
            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)
            {
                int row = Grid.GetRow((UIElement)child);
                int col = Grid.GetColumn((UIElement)child);
 
                var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0,
                    row == 0 ? (ignoreTop ? 0 : width) : 0,
                    (col == columnCount - 1 && ignoreRight) ? 0 : width,
                    width);
 
                if (child is UserControl control)
                {
                    control.BorderBrush = Brushes.Black;
                    control.BorderThickness = bt;
                }
 
                if (child is Label label)
                {
                    label.BorderBrush = Brushes.Black;
                    label.BorderThickness = bt;
                }
 
                if (child is TextBox box)
                {
                    box.BorderBrush = Brushes.Black;
                    box.BorderThickness = bt;
                }
            }
 
            /// 弃用
            //for (int i = 0; i < totalCount; i++)
            //{
            //    int row = i / columnCount;
            //    int col = i % columnCount;
 
            //    if (i > grid.Children.Count - 1) {
            //        continue;
            //    }
            //    var cell = grid.Children[i];
            //    var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0,
            //        row == 0 ? (ignoreTop ? 0 : width) : 0,
            //        (col == columnCount - 1 && ignoreRight) ? 0 : width,
            //        width);
 
            //    if (cell is UserControl child)
            //    {
            //        child.BorderBrush = Brushes.Black;
            //        child.BorderThickness = bt;
            //    }
 
            //    if (cell is Label label)
            //    {
            //        label.BorderBrush = Brushes.Black;
            //        label.BorderThickness = bt;
            //    }
 
            //    if (cell is TextBox box)
            //    {
            //        box.BorderBrush = Brushes.Black;
            //        box.BorderThickness = bt;
            //    }
            //}
            grid.Loaded -= GridInnerBorderLoaded;
        }
 
        private static void GridSpaceOnLoaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is Grid grid)) return;
            int space = GetBorderSpace(grid);
            if (space <= 0)
            {
                return;
            }
 
            var rowCount = grid.RowDefinitions.Count;
            var columnCount = grid.ColumnDefinitions.Count;
            int totalCount = rowCount * columnCount;
 
            for (int i = 0; i < totalCount; i++)
            {
                int row = i / columnCount;
                int col = i % columnCount;
 
                if (i > grid.Children.Count - 1)
                {
                    continue;
                }
                var cell = grid.Children[i];
 
                var bt = new Thickness(col == 0 ? space : 0, row == 0 ? space : 0, space, space);
 
                if (cell is UserControl child)
                {
                    child.Margin = bt;
                }
            }
            grid.Loaded -= GridSpaceOnLoaded;
        }
 
        private static void UniformGridOnLoad(object sender, RoutedEventArgs e)
        {
            if (!(sender is UniformGrid grid)) return;
            //var rowCount = grid.Rows;
            int columnCount = grid.Columns;
            int totalCount = grid.Children.Count;
            int rowCount = totalCount / columnCount;
            int width = 1;
 
            for (int i = 0; i < grid.Children.Count; i++)
            {
                int row = i / columnCount;
                int col = i % columnCount;
                var child = grid.Children[i];
 
                string baseData = "None";
                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");
 
                var bt = new Thickness(col == 0 ? (ignoreLeft ? 0 : width) : 0,
                    row == 0 ? (ignoreTop ? 0 : width) : 0,
                    (col == columnCount - 1 && ignoreRight) ? 0 : width,
                    (row == rowCount && ignoreBottom) ? 0 : width);
 
                if (child is UserControl control)
                {
                    control.BorderBrush = Brushes.Black;
                    control.BorderThickness = bt;
                }
 
                if (child is Label label)
                {
                    label.BorderBrush = Brushes.Black;
                    label.BorderThickness = bt;
                }
 
                if (child is TextBox box)
                {
                    box.BorderBrush = Brushes.Black;
                    box.BorderThickness = bt;
                }
            }
            grid.Loaded -= UniformGridOnLoad;
        }
    }
 
}