Skip to content

[Windows] - Fix Grid layout not updating when Row/Column changed dynamically #30206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Controls/src/Core/Layout/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ static void Invalidate(BindableObject bindable, object oldValue, object newValue
{
if (bindable is Grid grid)
{
#if WINDOWS
// Windows requires explicit parent invalidation for Grid.Row/Column/RowSpan/ColumnSpan changes.
// Unlike iOS/Android, Windows doesn't auto-propagate layout invalidation up the hierarchy,
// causing dynamic attached property changes to not arrange its children correctly.
if (grid.Parent is Grid containerGrid)
{
containerGrid.InvalidateMeasure();
return;
Copy link
Preview

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning immediately after invalidating the parent skips the grid's own InvalidateMeasure call. To guarantee both the grid and its container are re‐measured, consider calling grid.InvalidateMeasure() before or after invalidating the parent instead of exiting early.

Suggested change
return;
grid.InvalidateMeasure();

Copilot uses AI. Check for mistakes.

}
#endif
grid.InvalidateMeasure();
}
else if (bindable is Element element && element.Parent is Grid parentGrid)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 138 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20404.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 20404, "Dynamic Grid Row/Column changes don't trigger layout update on Windows until window resize", PlatformAffected.UWP)]
public class Issue20404 : TestContentPage
{
Grid dynamicGrid;
Button toggleRowButton;
Button toggleColumnButton;

protected override void Init()
{
Content = CreateMainGrid();
}

Grid CreateMainGrid()
{
Grid mainGrid = new Grid
{
RowSpacing = 8,
ColumnSpacing = 8,
RowDefinitions =
{
new RowDefinition { Height = GridLength.Star },
new RowDefinition { Height = GridLength.Star },
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Auto }
},
ColumnDefinitions =
{
new ColumnDefinition { Width = GridLength.Star },
new ColumnDefinition { Width = GridLength.Star }
}
};

AddStaticGrids(mainGrid);
AddDynamicGrid(mainGrid);
AddControlButtons(mainGrid);
return mainGrid;
}

void AddStaticGrids(Grid parent)
{
Grid topLeft = CreateStaticGrid("Static\n(0,0)", Colors.LightBlue);
Grid.SetRow(topLeft, 0);
Grid.SetColumn(topLeft, 0);
parent.Children.Add(topLeft);

Grid topRight = CreateStaticGrid("Static\n(0,1)", Colors.LightCoral);
Grid.SetRow(topRight, 0);
Grid.SetColumn(topRight, 1);
parent.Children.Add(topRight);

Grid bottomLeft = CreateStaticGrid("Static\n(1,0)", Colors.LightGreen);
Grid.SetRow(bottomLeft, 1);
Grid.SetColumn(bottomLeft, 0);
parent.Children.Add(bottomLeft);
}

Grid CreateStaticGrid(string labelText, Color backgroundColor)
{
Grid grid = new Grid { BackgroundColor = backgroundColor };
Label label = new Label
{
Text = labelText,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Colors.Black
};

grid.Children.Add(label);
return grid;
}

void AddDynamicGrid(Grid parent)
{
dynamicGrid = new Grid
{
BackgroundColor = Colors.DarkBlue,
AutomationId = "DynamicGrid"
};

Label label = new Label
{
Text = "DYNAMIC\nGRID",
TextColor = Colors.White,
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
AutomationId = "DynamicLabel"
};

dynamicGrid.Children.Add(label);
Grid.SetRow(dynamicGrid, 1);
Grid.SetColumn(dynamicGrid, 1);

parent.Children.Add(dynamicGrid);
}

void AddControlButtons(Grid parent)
{
toggleRowButton = new Button
{
Text = "Toggle Row (Current: 1)",
AutomationId = "ToggleRowButton"
};
toggleRowButton.Clicked += OnToggleRowClicked;

toggleColumnButton = new Button
{
Text = "Toggle Column (Current: 1)",
AutomationId = "ToggleColumnButton"
};
toggleColumnButton.Clicked += OnToggleColumnClicked;

StackLayout buttonStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Center,
Children = { toggleRowButton, toggleColumnButton }
};

Grid.SetRow(buttonStack, 3);
Grid.SetColumnSpan(buttonStack, 2);
parent.Children.Add(buttonStack);
}

void OnToggleRowClicked(object sender, EventArgs e)
{
Grid.SetRow(dynamicGrid, 0);
toggleRowButton.Text = $"Toggle Row (Current: 0)";
}

void OnToggleColumnClicked(object sender, EventArgs e)
{
Grid.SetColumn(dynamicGrid, 0);
toggleColumnButton.Text = $"Toggle Column (Current: 0)";
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue20404 : _IssuesUITest
{
public Issue20404(TestDevice testDevice) : base(testDevice) { }

public override string Issue => "Dynamic Grid Row/Column changes don't trigger layout update on Windows until window resize";

[Test]
[Category(UITestCategories.Layout)]
public void DynamicGridRowColumnChangeShouldInvalidate()
{
App.WaitForElement("ToggleRowButton");
App.Tap("ToggleRowButton");
VerifyScreenshot("AfterGridRowToggled");
App.WaitForElement("ToggleColumnButton");
App.Tap("ToggleColumnButton");
VerifyScreenshot("AfterGridColumnToggled");
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.