-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRow.cs
More file actions
207 lines (172 loc) · 7.13 KB
/
Row.cs
File metadata and controls
207 lines (172 loc) · 7.13 KB
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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using HandyControl.Data;
using HandyControl.Tools;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls;
public class Row : Panel
{
private ColLayoutStatus _layoutStatus;
private double _maxChildDesiredHeight;
private double _fixedWidth;
public static readonly DependencyProperty GutterProperty = DependencyProperty.Register(
nameof(Gutter), typeof(double), typeof(Row), new FrameworkPropertyMetadata(
ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsMeasure, null, OnGutterCoerce),
ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
private static object OnGutterCoerce(DependencyObject d, object basevalue) =>
ValidateHelper.IsInRangeOfPosDoubleIncludeZero(basevalue) ? basevalue : .0;
public double Gutter
{
get => (double) GetValue(GutterProperty);
set => SetValue(GutterProperty, value);
}
#region 父布局监控
// 当视觉父发生变化,订阅父的 SizeChanged / LayoutUpdated,确保在父确定尺寸后重新测量
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);
UnsubscribeParentSizeChanged(oldParent as FrameworkElement);
if (VisualParent is FrameworkElement newParent)
{
// 如果父已经有具体宽度,可以立即 InvalidateMeasure
if (newParent.ActualWidth > 0)
{
InvalidateMeasure();
}
else
{
// 等待父首次布局完成
newParent.SizeChanged += Parent_SizeChanged;
newParent.LayoutUpdated += Parent_LayoutUpdated;
}
}
}
private void UnsubscribeParentSizeChanged(FrameworkElement parent)
{
if (parent == null) return;
parent.SizeChanged -= Parent_SizeChanged;
parent.LayoutUpdated -= Parent_LayoutUpdated;
}
private void Parent_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (sender is FrameworkElement parent && parent.ActualWidth > 0)
{
UnsubscribeParentSizeChanged(parent);
Dispatcher.BeginInvoke(new Action(InvalidateMeasure), DispatcherPriority.Render);
}
}
private void Parent_LayoutUpdated(object sender, EventArgs e)
{
if (sender is FrameworkElement parent && parent.ActualWidth > 0)
{
UnsubscribeParentSizeChanged(parent);
Dispatcher.BeginInvoke(new Action(InvalidateMeasure), DispatcherPriority.Render);
}
}
#endregion
protected override Size MeasureOverride(Size constraint)
{
var gutter = Gutter;
// --- 防止无限约束 ---
if (double.IsInfinity(constraint.Width) || double.IsNaN(constraint.Width) || constraint.Width <= 0)
{
if (Parent is FrameworkElement parent && parent.ActualWidth > 0)
{
constraint = new Size(parent.ActualWidth, constraint.Height);
}
else
{
// 给出一个合理的默认宽度,避免返回 Infinity
constraint = new Size(100, constraint.Height);
// 延迟重新测量,等待父尺寸稳定
Dispatcher.BeginInvoke(new Action(InvalidateMeasure), DispatcherPriority.Render);
}
}
// 高度也进行防御
if (double.IsInfinity(constraint.Height) || double.IsNaN(constraint.Height))
constraint = new Size(constraint.Width, 100);
var localLayoutStatus = ColLayout.GetLayoutStatus(constraint.Width);
var totalCellCount = 0;
var totalRowCount = 1;
_fixedWidth = 0;
_maxChildDesiredHeight = 0;
var cols = InternalChildren.OfType<Col>().ToList();
// 先测量固定或不参与栅格的子元素
foreach (var child in cols)
{
var cellCount = child.GetLayoutCellCount(localLayoutStatus);
if (cellCount == 0 || child.IsFixed)
{
child.Measure(constraint);
_maxChildDesiredHeight = Math.Max(_maxChildDesiredHeight, child.DesiredSize.Height);
_fixedWidth += child.DesiredSize.Width + gutter;
}
}
var availableWidth = Math.Max(0, constraint.Width - _fixedWidth + gutter);
var itemWidth = availableWidth / ColLayout.ColMaxCellCount;
foreach (var child in cols)
{
var cellCount = child.GetLayoutCellCount(localLayoutStatus);
if (cellCount > 0 && !child.IsFixed)
{
totalCellCount += cellCount;
var availableChildWidth = Math.Max(0, cellCount * itemWidth - gutter);
child.Measure(new Size(availableChildWidth, constraint.Height));
_maxChildDesiredHeight = Math.Max(_maxChildDesiredHeight, child.DesiredSize.Height);
if (totalCellCount > ColLayout.ColMaxCellCount)
{
totalCellCount = cellCount;
totalRowCount++;
}
}
}
// --- 返回安全尺寸 ---
var returnWidth = constraint.Width;
var returnHeight = _maxChildDesiredHeight * totalRowCount;
if (double.IsInfinity(returnWidth) || double.IsNaN(returnWidth) || returnWidth <= 0)
{
if (Parent is FrameworkElement p && p.ActualWidth > 0)
returnWidth = p.ActualWidth;
else
returnWidth = 100; // fallback
}
if (double.IsInfinity(returnHeight) || double.IsNaN(returnHeight) || returnHeight < 0)
returnHeight = 0;
return new Size(returnWidth, returnHeight);
}
protected override Size ArrangeOverride(Size finalSize)
{
var gutter = Gutter;
var totalCellCount = 0;
var cols = InternalChildren.OfType<Col>().ToList();
// 再次设置布局状态,供 Arrange 使用
_layoutStatus = ColLayout.GetLayoutStatus(finalSize.Width);
var itemWidth = (finalSize.Width - _fixedWidth + gutter) / ColLayout.ColMaxCellCount;
itemWidth = Math.Max(0, itemWidth);
var childBounds = new Rect(0, 0, 0, _maxChildDesiredHeight);
foreach (var child in cols)
{
if (!child.IsVisible)
continue;
var cellCount = child.GetLayoutCellCount(_layoutStatus);
totalCellCount += cellCount;
var childWidth = (cellCount > 0 && !child.IsFixed)
? Math.Max(0, cellCount * itemWidth - gutter)
: child.DesiredSize.Width;
childBounds.Width = childWidth;
childBounds.X += child.Offset * itemWidth;
if (totalCellCount > ColLayout.ColMaxCellCount)
{
childBounds.X = 0;
childBounds.Y += _maxChildDesiredHeight;
totalCellCount = cellCount;
}
child.Arrange(childBounds);
childBounds.X += childWidth + gutter;
}
return finalSize;
}
}