Skip to content

Commit d7c83c9

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 c9173e8 commit d7c83c9

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
@@ -77,17 +77,31 @@ protected override Size MeasureOverride(Size availableSize)
7777

7878
double maxHeight = 0;
7979

80-
if (Children.Count > 0)
80+
// If we don't have a grid, just layout children like a horizontal StackPanel.
81+
if (_parentPanel is null)
8182
{
82-
// If we don't have a grid, just measure first child to get row height and take available space
83-
if (_parentPanel is null)
83+
double totalWidth = 0;
84+
85+
for (int i = 0; i < Children.Count; i++)
8486
{
85-
Children[0].Measure(availableSize);
86-
return new Size(availableSize.Width, Children[0].DesiredSize.Height);
87+
var child = Children[i];
88+
if (child?.Visibility != Visibility.Visible)
89+
continue;
90+
91+
child.Measure(availableSize);
92+
93+
totalWidth += child.DesiredSize.Width;
94+
maxHeight = Math.Max(maxHeight, child.DesiredSize.Height);
8795
}
96+
97+
return new Size(totalWidth, maxHeight);
98+
}
99+
100+
if (Children.Count > 0)
101+
{
88102
// Handle DataTable Parent
89-
else if (_parentTable != null
90-
&& _parentTable.Children.Count == Children.Count)
103+
if (_parentTable != null &&
104+
_parentTable.Children.Count == Children.Count)
91105
{
92106
// TODO: Need to check visibility
93107
// Measure all children since we need to determine the row's height at minimum
@@ -170,19 +184,41 @@ protected override Size MeasureOverride(Size availableSize)
170184

171185
protected override Size ArrangeOverride(Size finalSize)
172186
{
187+
// If we don't have a grid, just layout children like a horizontal StackPanel.
188+
if (_parentPanel is null)
189+
{
190+
double x = 0;
191+
192+
for (int i = 0; i < Children.Count; i++)
193+
{
194+
var child = Children[i];
195+
if (child?.Visibility != Visibility.Visible)
196+
continue;
197+
198+
double width = child.DesiredSize.Width;
199+
200+
child.Arrange(new Rect(x, 0, width, finalSize.Height));
201+
202+
x += width;
203+
}
204+
205+
return new Size(x, finalSize.Height);
206+
}
207+
173208
int column = 0;
174-
double x = 0;
175209

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

179-
double width = 0;
180-
181213
if (_parentPanel != null)
182214
{
215+
double x = 0;
216+
183217
int i = 0;
184218
foreach (UIElement child in Children.Where(static e => e.Visibility == Visibility.Visible))
185219
{
220+
double width;
221+
186222
if (_parentPanel is Grid grid &&
187223
column < grid.ColumnDefinitions.Count)
188224
{

0 commit comments

Comments
 (0)