From 998149c9e5732befb39c622ca2b599b66db28975 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 27 May 2019 13:31:38 +0800 Subject: [PATCH] Calculate ActualWidth/Height for Row/ColDefinitions so that GridSplitter can work. --- src/Avalonia.Controls/GridWPF.cs | 78 ++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Controls/GridWPF.cs b/src/Avalonia.Controls/GridWPF.cs index e18d70c840..aa08dd8469 100644 --- a/src/Avalonia.Controls/GridWPF.cs +++ b/src/Avalonia.Controls/GridWPF.cs @@ -32,8 +32,8 @@ namespace Avalonia.Controls internal bool HasStarCellsU; internal bool HasStarCellsV; internal bool HasGroup3CellsInAutoRows; - internal bool ColumnDefinitionsDirty; - internal bool RowDefinitionsDirty; + internal bool ColumnDefinitionsDirty = true; + internal bool RowDefinitionsDirty = true; // index of the first cell in first cell group internal int CellGroup1; @@ -284,13 +284,13 @@ namespace Avalonia.Controls if (_rowDefinitions.Count > 0) DefinitionsV = _rowDefinitions.Cast().ToArray(); else - DefinitionsV = new DefinitionBase[1] { new ColumnDefinition() }; + DefinitionsV = new DefinitionBase[1] { new RowDefinition() }; _rowDefinitions.CollectionChanged += (_, e) => { if (_rowDefinitions.Count == 0) { - DefinitionsV = new DefinitionBase[1] { new ColumnDefinition() }; + DefinitionsV = new DefinitionBase[1] { new RowDefinition() }; } else { @@ -451,14 +451,23 @@ namespace Avalonia.Controls private void ValidateColumnDefinitionsStructure() { - if (DefinitionsU == null || DefinitionsU?.Count() == 0) - DefinitionsU = new DefinitionBase[1] { new ColumnDefinition() }; + if (ColumnDefinitionsDirty) + { + if (DefinitionsU == null) + DefinitionsU = new DefinitionBase[1] { new ColumnDefinition() }; + ColumnDefinitionsDirty = false; + } } private void ValidateRowDefinitionsStructure() { - if (DefinitionsV == null || DefinitionsV?.Count() == 0) - DefinitionsV = new DefinitionBase[1] { new RowDefinition() }; + if (RowDefinitionsDirty) + { + if (DefinitionsV == null) + DefinitionsV = new DefinitionBase[1] { new RowDefinition() }; + + RowDefinitionsDirty = false; + } } /// @@ -521,11 +530,60 @@ namespace Avalonia.Controls { SetValid(); } + + for (var i = 0; i < ColumnDefinitions.Count; i++) + { + ColumnDefinitions[i].ActualWidth = GetFinalColumnDefinitionWidth(i); + } + + for (var i = 0; i < RowDefinitions.Count; i++) + { + RowDefinitions[i].ActualHeight = GetFinalRowDefinitionHeight(i); + } + return (arrangeSize); } /// - /// Invalidates grid caches and makes the grid dirty for measure. + /// Returns final width for a column. + /// + /// + /// Used from public ColumnDefinition ActualWidth. Calculates final width using offset data. + /// + internal double GetFinalColumnDefinitionWidth(int columnIndex) + { + double value = 0.0; + + // actual value calculations require structure to be up-to-date + if (!ColumnDefinitionsDirty) + { + value = DefinitionsU[(columnIndex + 1) % DefinitionsU.Length].FinalOffset; + if (columnIndex != 0) { value -= DefinitionsU[columnIndex].FinalOffset; } + } + return (value); + } + + /// + /// Returns final height for a row. + /// + /// + /// Used from public RowDefinition ActualHeight. Calculates final height using offset data. + /// + internal double GetFinalRowDefinitionHeight(int rowIndex) + { + double value = 0.0; + + // actual value calculations require structure to be up-to-date + if (!RowDefinitionsDirty) + { + value = DefinitionsV[(rowIndex + 1) % DefinitionsV.Length].FinalOffset; + if (rowIndex != 0) { value -= DefinitionsV[rowIndex].FinalOffset; } + } + return (value); + } + + /// + /// Invalidates grid caches and makes the grid dirty for measure. /// internal void Invalidate() { @@ -2695,7 +2753,7 @@ namespace Avalonia.Controls var grid = this.GetVisualParent(); if (grid == null - || !grid.ShowGridLines + || !grid.ShowGridLines || grid.IsTrivialGrid) { return;