Skip to content

Commit 3933681

Browse files
committed
DataRow lay out children like a horizontal StackPanel if DataTable is not present
This added behavior just covers a corner case. If a user fails to place corresponding DataTable at correct position, displaying somethings is better than nothing. Each rows layout their columns independent, because there's no main controller to remember the column widths, and then it looks like a horizontal StackPanel.
1 parent dd0285d commit 3933681

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

components/DataTable/src/DataTable/DataRow.cs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,31 @@ protected override Size MeasureOverride(Size availableSize)
8484

8585
double maxHeight = 0;
8686

87-
if (Children.Count > 0)
87+
// If we don't have a grid, just layout children like a horizontal StackPanel.
88+
if (_parentPanel is null)
8889
{
89-
// If we don't have a grid, just measure first child to get row height and take available space
90-
if (_parentPanel is null)
90+
double totalWidth = 0;
91+
92+
for (int i = 0; i < Children.Count; i++)
9193
{
92-
Children[0].Measure(availableSize);
93-
return new Size(availableSize.Width, Children[0].DesiredSize.Height);
94+
var child = Children[i];
95+
if (child?.Visibility != Visibility.Visible)
96+
continue;
97+
98+
child.Measure(availableSize);
99+
100+
totalWidth += child.DesiredSize.Width;
101+
maxHeight = Math.Max(maxHeight, child.DesiredSize.Height);
94102
}
103+
104+
return new Size(totalWidth, maxHeight);
105+
}
106+
107+
if (Children.Count > 0)
108+
{
95109
// Handle DataTable Parent
96-
else if (_parentTable != null
97-
&& _parentTable.Children.Count == Children.Count)
110+
if (_parentTable != null &&
111+
_parentTable.Children.Count == Children.Count)
98112
{
99113
// TODO: Need to check visibility
100114
// Measure all children since we need to determine the row's height at minimum
@@ -178,19 +192,41 @@ protected override Size MeasureOverride(Size availableSize)
178192
/// <inheritdoc/>
179193
protected override Size ArrangeOverride(Size finalSize)
180194
{
195+
// If we don't have a grid, just layout children like a horizontal StackPanel.
196+
if (_parentPanel is null)
197+
{
198+
double x = 0;
199+
200+
for (int i = 0; i < Children.Count; i++)
201+
{
202+
var child = Children[i];
203+
if (child?.Visibility != Visibility.Visible)
204+
continue;
205+
206+
double width = child.DesiredSize.Width;
207+
208+
child.Arrange(new Rect(x, 0, width, finalSize.Height));
209+
210+
x += width;
211+
}
212+
213+
return new Size(x, finalSize.Height);
214+
}
215+
181216
int column = 0;
182-
double x = 0;
183217

184218
// Try and grab Column Spacing from DataTable, if not a parent Grid, if not 0.
185219
double spacing = _parentTable?.ColumnSpacing ?? (_parentPanel as Grid)?.ColumnSpacing ?? 0;
186220

187-
double width = 0;
188-
189221
if (_parentPanel != null)
190222
{
223+
double x = 0;
224+
191225
int i = 0;
192226
foreach (UIElement child in Children.Where(static e => e.Visibility == Visibility.Visible))
193227
{
228+
double width;
229+
194230
if (_parentPanel is Grid grid &&
195231
column < grid.ColumnDefinitions.Count)
196232
{

0 commit comments

Comments
 (0)