diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Grid.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Grid.cs index b897692739ba..994952df851b 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Grid.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_Grid.cs @@ -164,6 +164,60 @@ await RunOnUIThread.ExecuteAsync(() => }); } + [TestMethod] + [RunsOnUIThread] + public async Task When_ColumnDefinition_Width_Changed() + { + var outerShell = new Grid { Width = 290, Height = 220 }; + + var colDef0 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }; + var colDef1 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }; + var SUT = new Grid + { + ColumnDefinitions = + { + colDef0, + colDef1, + } + }; + outerShell.Children.Add(SUT); + AddChild(SUT, new Border { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }, 0, 0); + AddChild(SUT, new Border { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }, 0, 1); + + TestServices.WindowHelper.WindowContent = outerShell; + await TestServices.WindowHelper.WaitForLoaded(SUT); + + const double expectedColWidth = 290 / 2; + Assert.AreEqual(expectedColWidth, colDef0.ActualWidth); + Assert.AreEqual(expectedColWidth, colDef1.ActualWidth); + + colDef0.Width = new GridLength(80, GridUnitType.Pixel); + Assert.AreEqual(expectedColWidth, colDef0.ActualWidth); + Assert.AreEqual(expectedColWidth, colDef1.ActualWidth); + + await TestServices.WindowHelper.WaitForRelayouted(SUT); + + Assert.AreEqual(80, colDef0.ActualWidth); + Assert.AreEqual(210, colDef1.ActualWidth); + } + + private static void AddChild(Grid parent, FrameworkElement child, int row, int col, int? rowSpan = null, int? colSpan = null) + { + Grid.SetRow(child, row); + Grid.SetColumn(child, col); + + if (rowSpan is { } rs) + { + Grid.SetRowSpan(child, rs); + } + if (colSpan is { } cs) + { + Grid.SetColumnSpan(child, cs); + } + + parent.Children.Add(child); + } + private async Task WaitForMeasure(FrameworkElement view, int timeOutMs = 1000) { var isMeasured = false; diff --git a/src/Uno.UI/UI/Xaml/Controls/Grid/ColumnDefinition.cs b/src/Uno.UI/UI/Xaml/Controls/Grid/ColumnDefinition.cs index b6246e7ca7b4..f1ad4081c021 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Grid/ColumnDefinition.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Grid/ColumnDefinition.cs @@ -19,9 +19,8 @@ public ColumnDefinition() this.RegisterDisposablePropertyChangedCallback((i, p, args) => { Changed?.Invoke(this, EventArgs.Empty); - SetDefaultState(); + InvalidateDefinition(); }); - SetDefaultState(); } #region Width DependencyProperty @@ -77,13 +76,12 @@ public double ActualWidth #region internal DefinitionBase - private void SetDefaultState() + private void InvalidateDefinition() { - _effectiveMinSize = default; - _measureArrangeSize = default; - _sizeCache = default; - _finalOffset = default; - _effectiveUnitType = GridUnitType.Auto; + if (this.GetParent() is Grid parentGrid) + { + parentGrid.InvalidateDefinitions(); + } } private double _effectiveMinSize; diff --git a/src/Uno.UI/UI/Xaml/Controls/Grid/Grid.h.cs b/src/Uno.UI/UI/Xaml/Controls/Grid/Grid.h.cs index 0634364c9590..f2d394d24dd9 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Grid/Grid.h.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Grid/Grid.h.cs @@ -260,7 +260,7 @@ bool HasGridFlags(GridFlags mask) // return DependencyObjectTraits.Index; //} - void InvalidateDefinitions() + internal void InvalidateDefinitions() { SetGridFlags(GridFlags.DefinitionsChanged); InvalidateMeasure(); diff --git a/src/Uno.UI/UI/Xaml/Controls/Grid/RowDefinition.cs b/src/Uno.UI/UI/Xaml/Controls/Grid/RowDefinition.cs index 15e20b001588..86c79bcc107c 100644 --- a/src/Uno.UI/UI/Xaml/Controls/Grid/RowDefinition.cs +++ b/src/Uno.UI/UI/Xaml/Controls/Grid/RowDefinition.cs @@ -18,9 +18,8 @@ public RowDefinition() this.RegisterDisposablePropertyChangedCallback((i, p, args) => { Changed?.Invoke(this, EventArgs.Empty); - SetDefaultState(); + InvalidateDefinition(); }); - SetDefaultState(); } #region Height DependencyProperty @@ -78,13 +77,12 @@ public double ActualHeight #region internal DefinitionBase - private void SetDefaultState() + private void InvalidateDefinition() { - _effectiveMinSize = default; - _measureArrangeSize = default; - _sizeCache = default; - _finalOffset = default; - _effectiveUnitType = GridUnitType.Auto; + if (this.GetParent() is Grid parentGrid) + { + parentGrid.InvalidateDefinitions(); + } } private double _effectiveMinSize;