From 878012ecd01b314ed46c07eaa17f7af7d23eb3ef Mon Sep 17 00:00:00 2001 From: sdoroff Date: Thu, 20 Aug 2020 12:35:17 -0400 Subject: [PATCH] DataGridRowDetails update height Updates the height of a DataGridRowDetails when its content changes size --- src/Avalonia.Controls.DataGrid/DataGridRow.cs | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Controls.DataGrid/DataGridRow.cs b/src/Avalonia.Controls.DataGrid/DataGridRow.cs index d5115c983a..1d5b0c884f 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridRow.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridRow.cs @@ -917,23 +917,23 @@ namespace Avalonia.Controls //TODO Cleanup double? _previousDetailsHeight = null; - + //TODO Animation - private void DetailsContent_SizeChanged(Rect newValue) + private void DetailsContent_HeightChanged(double newValue) { if (_previousDetailsHeight.HasValue) { var oldValue = _previousDetailsHeight.Value; - _previousDetailsHeight = newValue.Height; - if (newValue.Height != oldValue && newValue.Height != _detailsDesiredHeight) + _previousDetailsHeight = newValue; + if (newValue != oldValue && newValue != _detailsDesiredHeight) { if (AreDetailsVisible && _appliedDetailsTemplate != null) { // Update the new desired height for RowDetails - _detailsDesiredHeight = newValue.Height; + _detailsDesiredHeight = newValue; - _detailsElement.ContentHeight = newValue.Height; + _detailsElement.ContentHeight = newValue; // Calling this when details are not visible invalidates during layout when we have no work // to do. In certain scenarios, this could cause a layout cycle @@ -943,19 +943,29 @@ namespace Avalonia.Controls } else { - _previousDetailsHeight = newValue.Height; + _previousDetailsHeight = newValue; } } - private void DetailsContent_BoundsChanged(Rect newValue) + + private void DetailsContent_SizeChanged(Rect newValue) { - if(_detailsContent != null) - DetailsContent_SizeChanged(newValue.Inflate(_detailsContent.Margin)); + DetailsContent_HeightChanged(newValue.Height); } private void DetailsContent_MarginChanged(Thickness newValue) { if (_detailsContent != null) DetailsContent_SizeChanged(_detailsContent.Bounds.Inflate(newValue)); } + private void DetailsContent_LayoutUpdated(object sender, EventArgs e) + { + if (_detailsContent != null) + { + var margin = _detailsContent.Margin; + var height = _detailsContent.DesiredSize.Height + margin.Top + margin.Bottom; + + DetailsContent_HeightChanged(height); + } + } //TODO Animation // Sets AreDetailsVisible on the row and animates if necessary @@ -1035,12 +1045,26 @@ namespace Avalonia.Controls if (_detailsContent != null) { - _detailsContentSizeSubscription = - System.Reactive.Disposables.StableCompositeDisposable.Create( - _detailsContent.GetObservable(BoundsProperty) - .Subscribe(DetailsContent_BoundsChanged), + if (_detailsContent is Layout.Layoutable layoutableContent) + { + layoutableContent.LayoutUpdated += DetailsContent_LayoutUpdated; + + _detailsContentSizeSubscription = + System.Reactive.Disposables.StableCompositeDisposable.Create( + System.Reactive.Disposables.Disposable.Create(() => layoutableContent.LayoutUpdated -= DetailsContent_LayoutUpdated), + _detailsContent.GetObservable(MarginProperty) + .Subscribe(DetailsContent_MarginChanged)); + + + } + else + { + _detailsContentSizeSubscription = _detailsContent.GetObservable(MarginProperty) - .Subscribe(DetailsContent_MarginChanged)); + .Subscribe(DetailsContent_MarginChanged); + + } + _detailsElement.Children.Add(_detailsContent); } }