alielie
2025-11-25 006c98e5650b530e5220f279c1e803fefe69d40d
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
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
 
namespace HMI.UI.DpUtil
{
    public class UniformGridHelper
    {
        public static double GetInnerBorder(DependencyObject obj)
        {
            return (double)obj.GetValue(InnerBorderProperty);
        }
 
        public static void SetInnerBorder(DependencyObject obj, object value)
        {
            double dv = double.Parse(value.ToString());
            obj.SetValue(InnerBorderProperty, dv);
        }
 
        public static readonly DependencyProperty InnerBorderProperty =
            DependencyProperty.RegisterAttached("InnerBorder", typeof(object), typeof(UniformGrid),
                new PropertyMetadata(0, OnInnerBorderChanged));
 
        public static Brush GetInnerBorderBrush(DependencyObject obj)
        {
            return (Brush)obj.GetValue(InnerBorderBrushProperty);
        }
        public static void SetInnerBorderBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(InnerBorderBrushProperty, value);
        }
        public static readonly DependencyProperty InnerBorderBrushProperty =
            DependencyProperty.RegisterAttached("InnerBorderBrush", typeof(Brush), typeof(UniformGrid),
                new PropertyMetadata(null, OnInnerBorderChanged));
 
        public static string GetInnerBorderIgnore(DependencyObject obj)
        {
            return (string)obj.GetValue(InnerBorderIgnoreProperty);
        }
        public static void SetInnerBorderIgnore(DependencyObject obj, string value)
        {
            obj.SetValue(InnerBorderIgnoreProperty, value);
        }
 
        public static readonly DependencyProperty InnerBorderIgnoreProperty =
            DependencyProperty.RegisterAttached("InnerBorderIgnore", typeof(string),
                typeof(UniformGrid), new PropertyMetadata("None", OnInnerBorderChanged));
 
        private static void OnInnerBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UniformGrid grid)
            {
                if (e.Property == InnerBorderProperty || e.Property == InnerBorderIgnoreProperty)
                {
                    grid.Loaded += GridInnerBorder_Loaded;
                    return;
                }
            }
        }
 
        private static void GridInnerBorder_Loaded(object sender, RoutedEventArgs e)
        {
            UniformGrid grid = sender as UniformGrid;
            RefreshGridInnerBorder(grid);
            grid.Loaded -= GridInnerBorder_Loaded;
        }
 
        public static void RefreshGridInnerBorder(UniformGrid grid)
        {
            double width = GetInnerBorder(grid);
            if (width <= 0) return;
            var rowCount = grid.Rows;
            var columnCount = grid.Columns;
            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");
 
            rowCount = (grid.Children.Count / columnCount)
                        + (grid.Children.Count % columnCount > 0 ? 1 : 0);
            var brush = GetInnerBorderBrush(grid) ?? Brushes.Black;
 
            for (int i = 0; i < grid.Children.Count; i++)
            {
                var child = grid.Children[i];
 
                if (child is Control control)
                {
 
                    int col = i % columnCount;
                    int row = i / columnCount;
 
                    double left = (col == 0 && ignoreLeft) ? 0 : width;
                    double top = (row == 0 && ignoreTop) ? 0 : width;
 
                    double right = ignoreRight ? 0 : ((col == columnCount - 1) ? width : 0);
                    double bottom = ignoreBottom ? 0 : ((row == rowCount - 1) ? width : 0);
 
                    var bt = new Thickness(left, top, right, bottom);
 
                    control.BorderBrush = brush;
                    control.BorderThickness = bt;
                }
 
            }
        }
 
        public static void RefreshLineSize(UniformGrid grid)
        {
            Thickness parentThickness = default;
            Brush brush = default;
            var parent = grid.Parent;
            if (parent is System.Windows.Controls.Control parentControl)
            {
                parentThickness = parentControl.BorderThickness;
                brush = parentControl.BorderBrush;
            }
            else if (parent is Border border)
            {
                parentThickness = border.BorderThickness;
                brush = border.BorderBrush;
            }
 
            //var value = GetLineSize(grid);
            //if (value == null) return;
            //double size = (double)value;
 
            int totalCount = grid.Children.Count;
            var columnCount = grid.Columns;
            int rowCount = grid.Rows;
 
            for (int i = 0; i < totalCount; i++)
            {
                int row = i / columnCount;
                int rest = i % columnCount;
 
 
                var cell = grid.Children[i];
 
                var bt = new Thickness(rest == 0 ? 0 : parentThickness.Left,
                    row == 0 ? 0 : parentThickness.Top,
                    rest == columnCount - 1 ? 0 : parentThickness.Right,
                    row == rowCount - 1 ? 0 : parentThickness.Bottom);
 
                if (cell is Control child)
                {
                    child.BorderThickness = bt;
                    child.BorderBrush = brush;
                }
            }
        }
 
        public static void RefreshDefault(UniformGrid grid, ControlTemplate template)
        {
            int totalCount = grid.Columns * grid.Rows;
 
            int rest = totalCount - grid.Children.Count;
            if (rest <= 0) return;
 
            for (int i = 0; i < rest; i++)
            {
                var control = new Control
                {
                    Template = template
                };
                grid.Children.Add(control);
            }
        }
 
        public static ControlTemplate GetDefaultTemplate(DependencyObject obj)
        {
            return (ControlTemplate)obj.GetValue(DefaultTemplateProperty);
        }
        public static void SetDefaultTemplate(DependencyObject obj, ControlTemplate value)
        {
            obj.SetValue(DefaultTemplateProperty, value);
        }
        public static readonly DependencyProperty DefaultTemplateProperty =
            DependencyProperty.RegisterAttached("DefaultTemplate", typeof(ControlTemplate), typeof(UniformGrid),
                new PropertyMetadata(null, OnDefaultTemplateChanged));
 
        private static void OnDefaultTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is UniformGrid grid)
            {
                if (e.Property == DefaultTemplateProperty)
                {
                    var tem = GetDefaultTemplate(grid);
                    if (tem == null) return;
 
                    //var rowCount = grid.Rows;
                    int columnCount = grid.Columns;
                    int totalCount = grid.Children.Count;
                    int rowCount = totalCount / columnCount;
                    int last = totalCount % columnCount;
                    if (last == 0)
                    {
                        return;
                    }
                    int addCount = columnCount - last;
 
                    for (int i = 0; i < addCount; i++)
                    {
                        Button u = new Button
                        {
                            Template = tem
                        };
                        grid.Children.Add(u);
                    }
                }
            }
        }
    }
}