diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs
index 98b3b13c17..8a5ac4e495 100644
--- a/src/Avalonia.Controls/TextBox.cs
+++ b/src/Avalonia.Controls/TextBox.cs
@@ -1879,6 +1879,37 @@ namespace Avalonia.Controls
return text.Substring(start, end - start);
}
+ ///
+ /// Returns the sum of any vertical whitespace added between the and in the control template.
+ ///
+ /// The total vertical whitespace.
+ private double GetVerticalSpaceBetweenScrollViewerAndPresenter()
+ {
+ var verticalSpace = 0.0;
+ if (_presenter != null)
+ {
+ Visual? visual = _presenter;
+ while ((visual != null) && (visual != this))
+ {
+ if (visual == _scrollViewer)
+ {
+ // ScrollViewer is a stopping point and should only include the Padding
+ verticalSpace += _scrollViewer.Padding.Top + _scrollViewer.Padding.Bottom;
+ break;
+ }
+
+ var margin = visual.GetValue(Layoutable.MarginProperty);
+ var padding = visual.GetValue(Decorator.PaddingProperty);
+
+ verticalSpace += margin.Top + padding.Top + padding.Bottom + margin.Bottom;
+
+ visual = visual.VisualParent;
+ }
+ }
+
+ return verticalSpace;
+ }
+
///
/// Raises both the and events.
///
@@ -2032,8 +2063,9 @@ namespace Avalonia.Controls
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var paragraphProperties = TextLayout.CreateTextParagraphProperties(typeface, fontSize, null, default, default, null, default, LineHeight, default);
var textLayout = new TextLayout(new MaxLinesTextSource(MaxLines), paragraphProperties);
+ var verticalSpace = GetVerticalSpaceBetweenScrollViewerAndPresenter();
- maxHeight = Math.Ceiling(textLayout.Height);
+ maxHeight = Math.Ceiling(textLayout.Height + verticalSpace);
}
_scrollViewer.SetCurrentValue(MaxHeightProperty, maxHeight);